我正在尝试调用我自己构建的 REST WS。下面是我在客户端的代码。在服务器端,这个调用完成得很好,我可以在调试器中看到我想看到的参数。方法是 POST,有一个 XML 参数,返回 JSON。我正在使用 Java 和泽西岛。
我正在使用这个版本的泽西岛:jersey-client-1.0.3.jar jersey-core-1.0.3.jar jersey-json-1.0.3.jar jersey-server-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;
}