RESTful WS - Jersey - 客户端解组问题

时间:2013-11-09 20:05:22

标签: java jersey jersey-client

我正在尝试调用我自己构建的REST WS。 下面是我在客户端的代码。 在服务器端,此调用完成, 我可以看到我想看的论点 在调试器中。方法是POST, 有一个XML参数,返回JSON。 我正在使用Java和Jersey。

我正在使用这个版本的泽西岛: 新泽西州的客户 - 1.0.3.jar 球衣核心 - 1.0.3.jar 新泽西州JSON-1.0.3.jar 球衣 - 服务器 - 1.0.3.jar

我无法轻易升级,这不取决于我。

package com.company.module.test;

import java.net.URI;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.company.common.DateUtil;
import com.company.module.input.AssetOperation;
import com.company.module.input.AssetOperationData;

public class MainProg002 {

    public static void main(String[] args) throws Exception {
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        WebResource service = client.resource(getBaseURI());

        AssetOperationData data = new AssetOperationData();
        AssetOperation op1 = new AssetOperation();
        op1.setAssetID("1234");
        op1.setDate(DateUtil.getDate(2013, 12, 22));
        op1.setOperation("pause");
        AssetOperation op2 = new AssetOperation();
        op2.setAssetID("5050");
        op2.setDate(DateUtil.getDate(2013, 12, 5));
        op2.setOperation("resume");
        data.getAssetOperations().add(op1);
        data.getAssetOperations().add(op2);

        service.path("Asset").entity(data, MediaType.APPLICATION_XML).post(AssetOperationData.class, data);
    }

    private static URI getBaseURI() {
        return UriBuilder.fromUri("http://localhost:8080/module/service").build();
    }
}

在我看来,问题是客户端无法解组某些对象 在客户端正确。以下是我得到的例外情况。我认为 它与XSD中的元素命名空间有关吗?!我不知道 如何告诉客户端使用特定的XSD。另外,我不确定是否 这是必需的,因为RESTful服务在这种情况下返回JSON。 此外,我很困惑的是,例外提到了“输入” 是参数中的元素,而不是返回值?!

Exception in thread "main" javax.ws.rs.WebApplicationException: javax.xml.bind.UnmarshalException
 - with linked exception:
[javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"input"). Expected elements are <{http://company.com/module/input}input>]
    at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:99)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:259)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:220)
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:561)
    at com.sun.jersey.api.client.WebResource.access$300(WebResource.java:69)
    at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:499)
    at com.company.module.test.MainProg002.main(MainProg002.java:36)
Caused by: javax.xml.bind.UnmarshalException
 - with linked exception:
[javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"input"). Expected elements are <{http://company.com/module/input}input>]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:435)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:372)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:342)
    at com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider.readFrom(JSONRootElementProvider.java:110)
    at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:97)
    ... 6 more
Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"input"). Expected elements are <{http://company.com/module/input}input>
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:662)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:258)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:253)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:120)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1063)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:498)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:480)
    at com.sun.xml.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:75)
    at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(StAXStreamConnector.java:246)
    at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:180)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:370)
    ... 9 more
Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"input"). Expected elements are <{http://company.com/module/input}input>
    ... 20 more
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF8

我会非常感谢任何帮助。

在客户端:

1 * Out-bound request
1 > POST http://localhost:8080/module/service/Asset
1 > Content-Type: application/xml
1 > 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><input xmlns="http://company.com/module/input"><item><assetID>1234</assetID><date>2014-01-22-05:00</date><operation>pause</operation></item><item><assetID>5050</assetID><date>2014-01-05-05:00</date><operation>resume</operation></item></input>
1 < 200
1 < Transfer-Encoding: chunked
1 < Content-Type: application/json
1 < Server: Apache-Coyote/1.1
1 < Date: Sat, 09 Nov 2013 20:23:22 GMT
1 < 
{"error":"","id":"10"}
1 * In-bound response

这就是我的方法的样子。

@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_JSON)
public AssetResult manageAssets(AssetOperationData data) {
    LogProvider.logInfo(this, "BEGIN");
    LogProvider.logInfo(this, "Found POST data = " + data);
    AssetResult result = new AssetResult();
    result.setId(10);
    LogProvider.logInfo(this, "END");
    return result;
}

1 个答案:

答案 0 :(得分:0)

我终于从方法中返回了JSON并略微更改了客户端。以下是最终运作的客户端。关键点是两个。 1)发布String.class; 2)使用Gson并使用Gson在客户端解组返回的AssetResult对象。

package com.company.module.test;

import java.net.URI;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import com.google.gson.Gson;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.LoggingFilter;
import com.company.common.DateUtil;
import com.company.module.input.AssetOperation;
import com.company.module.input.AssetOperationData;
import com.company.module.result.AssetResult;

public class MainProg002 {

    public static void main(String[] args) throws Exception {
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        client.addFilter(new LoggingFilter());

        WebResource service = client.resource(getBaseURI());

        AssetOperationData data = new AssetOperationData();
        AssetOperation op1 = new AssetOperation();
        op1.setAssetID("1234");
        op1.setDate(DateUtil.getDate(2013, 12, 22));
        op1.setOperation("pause");
        AssetOperation op2 = new AssetOperation();
        op2.setAssetID("5050");
        op2.setDate(DateUtil.getDate(2013, 12, 5));
        op2.setOperation("resume");
        data.getAssetOperations().add(op1);
        data.getAssetOperations().add(op2);

        String res = service.path("Asset").entity(data, MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_JSON).post(String.class);
        Gson gson = new Gson();
        AssetResult result = gson.fromJson(res, AssetResult.class);

        System.out.println("DONE!");
    }

    private static URI getBaseURI() {
        return UriBuilder.fromUri("http://localhost:8080/module/service").build();
    }
}