The following code uses the Jersey2 client (v 2.22.2) with the built-in Moxy JSON support. I am trying to marshal a generified wrapper class into JSON. As you can see below the json
property isn't unmarshalling as intended.
When I change the type for the json
property to Service
and 'ungenerify' the class everything works as intended but I would prefer to be able to do this with generics so I don't have to create a bunch of these wrapper classes for each type that needs to be wrapped in this fashion.
JSONWrapper.java
public class JSONWrapper<T> implements Serializable {
private T json;
public T getJson() { return this.json; }
public void setJson (T payload) { this.json = payload; }
public JSONWrapper() {}
}
Service.java
public class Service implements Serializable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
MyClient.java
public class MyClient {
private Client jerseyClient;
public void createService(Service item) {
Invocation.Builder invocationBuilder = buildRequest(SERVICE_PATH);
JSONWrapper<Service> wrapper = new JSONWrapper<>();
wrapper.setJson(item);
final Response response = invocationBuilder.post(Entity.entity(wrapper, MediaType.APPLICATION_JSON_TYPE));
}
public MyClient() {
this.jerseryClient = ClientBuilder.newBuilder().build();
}
public static void main(String[] args) {
MyClient myClient = new MyClient();
Service myService = new Service();
myService.setName("foo");
myClient.createService(myService);
}
}
With the logging filter turned on I am seeing that Moxy's marshalling of things looks like so:
{"json":"com.baz.Service@5674e1f2"}
When I want it to look like so
{"json":"{"name": "foo"}"}