javajerseyjersey-client

RESTful WS - Jersey - client side unmarshalling issue


I am trying to call a REST WS which I built myself. Below is my code on the client side. On the server side this call completes fine, I can see the arguments which I want to see in the debugger. The method is POST, has an XML argument, returns JSON. I am using Java and Jersey.

I am using this version of Jersey: jersey-client-1.0.3.jar jersey-core-1.0.3.jar jersey-json-1.0.3.jar jersey-server-1.0.3.jar

I cannot upgrade that easily, it's not up to me.

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();
    }
}

Seems to me the problem is then that the client cannot unmarshal some objects properly on the client side. Below is the exception which I get. I think it has something to do with element namespaces in XSD. I have no idea how to tell the client to use a particular XSD.

Also, I am not sure if that's needed at all, because the RESTful service returns JSON in this case. Also I am puzzled by the fact that the exception mentions "input" which is an element from the arguments, not from the return values!

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

On the client side:

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

This is how my method looks like.

@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;
}

Solution

  • I finally returned JSON from the method and slightly changed the client. Below is a client which finally works. The key points are two. 1) Post with String.class; 2) Use Gson and unmarshal the returned AssetResult object on the client side with Gson.

    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();
        }
    }