【问题标题】:Not able to map JSON response to POJO from com.sun.jersey.api.client.ClientResponse无法从 com.sun.jersey.api.client.ClientResponse 将 JSON 响应映射到 POJO
【发布时间】:2019-02-12 18:12:04
【问题描述】:

我正在尝试将 JSON 响应映射到 POJO,某些属性(属性以 @ 开头)无法映射,因此出现以下错误。

请找到 JSON 和 Class 文件,要对 POJO 进行哪些更改以映射下面提到的 JSON 的所有属性?

JSON

{
  "@customerId": "123456",
  "customerName": "Jobin",
  "orders": [
    {
      "orderId": "bvbundle002075",
      "address": {
        "@elid": "35475908"
      },

    }
  ]
}

Customer.java

import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement
@XmlType
public class CustomerData implements Serializable {

    private static final long serialVersionUID = 9163262900343406982L;

    private String customerId;
    private String customerName;
    private List<Order> orders;

    @XmlElement(name = "@customerId")
    public String getCustomerId() {
        return customerId;
    }

    public void setCustomerId(final String customerId) {
        this.customerId = customerId;
    }


    public String getCustomerName() {
        return customerId;
    }

    public void setCustomerName(final String customerName) {
        this.customerName = customerName;
    }

    public List<Order> getOrders() {
        return orders;
    }

    public void setExistingProducts(final List<Order> orders) {
        this.orders = orders;
    }
}

Order.java

class Order implements Serializable {

    private static final long serialVersionUID = 1L;
    private String orderId;
    private Address address;

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(final String orderId) {
        this.orderId = orderId;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(final Address address) {
        this.address = address;
    }
}

Address.java

class Address implements Serializable {

    private static final long serialVersionUID = 1L;

    private String elid;

    @XmlElement(name = "@elid")
    public String getElid() {
        return elid;
    }

    public void setElid(String elid) {
        this.elid = elid;
    }
}

错误日志

javax.ws.rs.WebApplicationException: javax.xml.bind.UnmarshalException
 - with linked exception:
[javax.xml.stream.XMLStreamException: java.lang.NullPointerException]
    at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:113)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:634)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:586)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
    at java.lang.Thread.run(Unknown Source)
Caused by: javax.xml.bind.UnmarshalException: null
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
    at com.sun.jersey.json.impl.BaseJSONUnmarshaller.unmarshalJAXBElementFromJSON(BaseJSONUnmarshaller.java:111)
    at com.sun.jersey.json.impl.BaseJSONUnmarshaller.unmarshalFromJSON(BaseJSONUnmarshaller.java:100)
    at com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider.readFrom(JSONRootElementProvider.java:154)
    at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:111)
    ... 144 common frames omitted
Caused by: javax.xml.stream.XMLStreamException: java.lang.NullPointerException
    at com.sun.jersey.json.impl.reader.XmlEventProvider.processTokens(XmlEventProvider.java:421)
    at com.sun.jersey.json.impl.reader.XmlEventProvider.readNext(XmlEventProvider.java:466)
    at com.sun.jersey.json.impl.reader.JsonXmlStreamReader.next(JsonXmlStreamReader.java:506)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(Unknown Source)
    ... 150 common frames omitted
Caused by: java.lang.NullPointerException: null
    at com.sun.jersey.json.impl.reader.XmlEventProvider.processTokens(XmlEventProvider.java:315)

【问题讨论】:

  • 您应该使用 Jackson 进行 JSON 处理。假设你使用 Jersey 1.x,添加 jersey-json 依赖并配置 POJOMappingFeature
  • 是的,我们使用的是 Jersy 1.x
  • 你搞定了吗?
  • 不,还没有......
  • 想发布您的 pom.xml、web.xml 和客户端代码。

标签: java xml jaxb jersey jersey-1.0


【解决方案1】:

问题是我试图将 xml 属性映射到 xml 元素。如果您转换响应中提到的上述 JSON,它将生成如下所示的 XML..

<?xml version="1.0" encoding="UTF-8"?>
<root customerId="123456">
   <customerName>Jobin</customerName>
   <orders>
      <element>
         <address elid="35475908" />
         <orderId>bvbundle002075</orderId>
      </element>
   </orders>
</root>

如您所见,customerIdelid 是 xml 属性而不是元素。

所以我不得不使用下面的方法来修复它,

@XmlAttribute(name = "customerNumber")  //will map to @customerNumber in JSON
@XmlAttribute(name = "elid")            //will map to @elid in JSON

【讨论】:

    猜你喜欢
    • 2014-04-12
    • 2021-08-25
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    相关资源
    最近更新 更多