Getting JSON out put from restful java client
i resolved the same question.if your exception is : unexpected element (uri:"", local:"id").........
don't forget add follow code:
DefaultClientConfig clientConfig = new DefaultClientConfig(); clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); Client client = Client.create(clientConfig);
then the code:
return resource.type(MediaType.APPLICATION_JSON_TYPE).get(new GenericType<List<MyClass>>(){});
will be ok.
1) Whoever is generating this error, is clearly expecting XML input. Not JSON. You need to change that ASAP:
javax.ws.rs.WebApplicationException: javax.xml.bind.UnmarshalException com.sun.istack.internal.SAXParseException2; <= javax.xml.bind and SAXParse are both XML-only: JSON not invited
2) The stuff in your screen shot (presumably Jersey?) is definitely OK.
3) I haven't followed the whole tutorial, and you haven't given enough information to tell where you went astray.
SUGGESTION:
Just retrace your steps in the tutorial, and make sure you're selecting "JSON" (not XML, and not SOAP) every step of the way.
=========== ADDENDUM ===========
OK - Thanx for the update. Here's where we're at:
1) This is the problem:
Exception in thread "main" javax.ws.rs.WebApplicationException: javax.xml.bind.UnmarshalException - with linked exception:[com.sun.istack.internal.SAXParseException2; lineNumber: 0; columnNumber: 0; unexpected element (uri:"", local:"id"). Expected elements are <{}person>] at com.sun.jersey.core.provider.jaxb.AbstractListElementProvider.readFrom(AbstractListElementProvider.java:251) at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:553) at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:523) at soatestclient.SOATestClient.main(SOATestClient.java:33)Caused by: javax.xml.bind.UnmarshalException
2) You said this stack traceback is coming from the client.
So your server is 100% OK - the ONLY thing you need to do is fix your client. Cool :)
3) The traceback shows the client is expecting XML ... but getting JSON instead.
So the ONLY thing you should need to fix is to tell your client "Hey: read JSON, not XML". Again - cool :)
4) How do you do that?
Well, for starters, you need to get rid of this line (if you haven't already):
// Bad, bad bad. Don't do this!|ClientResponse response = client.findAll_XML(ClientResponse.class);
5) You might want to change other parts of your client code - I don't know.
You might also want to change your client's configuration - I don't know that, either.
6) Suggestion: look at this other tutorial - it might point you in the right direction:
NOTE:
WHATEVER you need to do - it should be REALLY simple! Please review the link, review your code and your test client configuration ... and post back what you find!
Thank you in advance...
http://smoothexample.com/webservices/apache_cxf_rest_web_services_client.html
The above example gives sample client application using apache cxf, here client can consume both xml and json by providing the "Accept" header.
Also a simple example for cxf rest web service is also available here, which even returns both xml and json based on the "Accept" header.