Jersey Client - Variable types are ignored

186 views Asked by At

I am using the Jersey Client of Sun Version 1.19...

When I send a put request with JSON (but every other request type probably as well) it serializes Number-typed variables with quotes, instead of as plain numbers.

Example:

User.java

package de.topazmedia.component.api.rest.client.ressource;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
public class User {

Long id;
String username;


public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}    

public String getUsername() {
    return id;
}
public void setUsername(Long id) {
    this.id = id;
} 

}

Test.java

import javax.ws.rs.core.MediaType;

import org.json.JSONObject;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.WebResource.Builder;

public class Test {

public static void main(String[] args) {
User user = new User();

user.setId(1);
user.setUsername("Test");

ClientResponse response = null;
    
WebResource webResource = client.resource(http://192.168.23.56:9181/v1/users/" + user.getId());
    
Builder builder = webResource.header("Accept-Language", "de").type(MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON_TYPE);

response = builder.put(ClientResponse.class, user);
}
}

The "User JSON" I put as entity in the request is now translated to

{"id":"1","username":"Test"}

BUT it should be without the apostrphes at the id like this:

{"id":1,"username":"Test"}

What is wrong?

1

There are 1 answers

0
Steffen On BEST ANSWER

The solution of the problem ist quite simple.

Just define a POJO-Mapping in the ClientConfig when instantiating the Client Object:

ClientConfig cc = new DefaultClientConfig();

cc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);

client = Client.create(cc);