简体   繁体   中英

Jersey JaxB unmarshalling of type Object issue

I'm using jersey2 jaxrs client and default moxy as response mapper. Following json is my response json from the service.

{"key":"thekey","id":"the___id","value":{"imageUrl":"https://asdad","imageType":"asdsadasd"}}

https://api.myjson.com/bins/efe74

But in our business need, the value of "value" field cannot be determined. Sometimes it can be an array or an object or just a integet. So basically I don't know the type of the Object.

Due to this my DTO class is looks like this.

public class FieldData {
  private String id;
  private String key;
  private Object value;

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }


  public String getKey() {
    return key;
  }

  public void setKey(String value) {
    this.value = value;
  }

  public Object getValue() {
    return value;
  }

  public void setValue(Object value) {
    this.value = value;
  }
}

And by default jersey2 is using moxy and JAXB as response mapper. Now the problem is id and key values are mapped correctly as it knows the concrete type.

But value field translated as XML string in the client.

{
     "key":"thekey",
     "id":"the___id",
     "value": "<? xml version="1.0" encoding="UTF-8"?><value imageUrl=\"https://asdad\" imageType=\"asdsadasd\"><imageUrl></imageUrl></value>"
   }

I can understand that, because of the object type JAXB might not able to know the concrete type. But all I'm expecting is that this value field response should be in the JSON instead of this malformed XML string.

How to achieve this?

Looks like this is not possible with moxy as json mapper. So I've tried with Jackson and works fine. On client creation used the following.

ClientBuilder.newClient()
       .property("jersey.config.client.disableMoxyJson", true)
       .register(new JacksonJsonProvider(mapper)) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM