简体   繁体   English

Jersey JaxB 解组对象类型问题

[英]Jersey JaxB unmarshalling of type Object issue

I'm using jersey2 jaxrs client and default moxy as response mapper.我使用 jersey2 jaxrs 客户端和默认的 moxy 作为响应映射器。 Following json is my response json from the service.以下 json 是我来自服务的响应 json。

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

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

But in our business need, the value of "value" field cannot be determined.但是在我们的业务需求中,“value”字段的值是无法确定的。 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.因此,我的 DTO 类看起来像这样。

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. jersey2 默认使用 moxy 和 JAXB 作为响应映射器。 Now the problem is id and key values are mapped correctly as it knows the concrete type.现在问题是 id 和键值正确映射,因为它知道具体类型。

But value field translated as XML string in the client.但是 value 字段在客户端转换为 XML 字符串。

{
     "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.我可以理解,因为对象类型 JAXB 可能无法知道具体类型。 But all I'm expecting is that this value field response should be in the JSON instead of this malformed XML string.但我所期望的是这个值字段响应应该在 JSON 中,而不是这个格式错误的 XML 字符串。

How to achieve this?如何实现这一目标?

Looks like this is not possible with moxy as json mapper.看起来 moxy 作为 json 映射器是不可能的。 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)) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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