我正在使用REST(Jersey 1.8)开发Web服务。目前我正在使用XML在Java客户端和服务器之间进行通信。
我需要将其更改为JSON:我该怎么做?我有大量来自NetBeans的自动生成代码,并且不知道该做什么以及如何做。在测试服务时,它显示JSON数据。我无法做的是在main
方法中处理它。
这些是我遵循的教程
我的Java客户端main
方法:
public class SOATestClient {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
PersonJerseyClient client = new PersonJerseyClient();
ClientResponse response = client.findAll_XML(ClientResponse.class);
GenericType<List<Person>> genericType = new GenericType<List<Person>>() {
};
// Returns an ArrayList of Players from the web service
List<Person> data = new ArrayList<Person>();
data = (response.getEntity(genericType));
System.out.println("Retreiving and Displaying Players Details");
for (Person person : data) {
System.out.println("FirstName: " + person.getName());
System.out.println("ID : " + person.getId());
System.out.println(" Age : " + person.getAge());
}
client.close();
}
}
personjerseycilent
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jerseyclients;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
/**
* Jersey REST client generated for REST resource:PersonFacadeREST
* [entity.person]<br>
* USAGE:
* <pre>
* PersonJerseyClient client = new PersonJerseyClient();
* Object response = client.XXX(...);
* // do whatever with response
* client.close();
* </pre>
*
* @author rj45
*/
public class PersonJerseyClient {
private WebResource webResource;
private Client client;
private static final String BASE_URI = "http://localhost:8080/SOATestService/resources";
public PersonJerseyClient() {
com.sun.jersey.api.client.config.ClientConfig config = new com.sun.jersey.api.client.config.DefaultClientConfig();
client = Client.create(config);
webResource = client.resource(BASE_URI).path("entity.person");
}
public void remove(String id) throws UniformInterfaceException {
webResource.path(java.text.MessageFormat.format("{0}", new Object[]{id})).delete();
}
public String countREST() throws UniformInterfaceException {
WebResource resource = webResource;
resource = resource.path("count");
return resource.accept(javax.ws.rs.core.MediaType.TEXT_PLAIN).get(String.class);
}
public <T> T findAll_XML(Class<T> responseType) throws UniformInterfaceException {
WebResource resource = webResource;
return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML).get(responseType);
}
public <T> T findAll_JSON(Class<T> responseType) throws UniformInterfaceException {
WebResource resource = webResource;
return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
}
public void edit_XML(Object requestEntity) throws UniformInterfaceException {
webResource.type(javax.ws.rs.core.MediaType.APPLICATION_XML).put(requestEntity);
}
public void edit_JSON(Object requestEntity) throws UniformInterfaceException {
webResource.type(javax.ws.rs.core.MediaType.APPLICATION_JSON).put(requestEntity);
}
public void create_XML(Object requestEntity) throws UniformInterfaceException {
webResource.type(javax.ws.rs.core.MediaType.APPLICATION_XML).post(requestEntity);
}
public void create_JSON(Object requestEntity) throws UniformInterfaceException {
webResource.type(javax.ws.rs.core.MediaType.APPLICATION_JSON).post(requestEntity);
}
public <T> T findRange_XML(Class<T> responseType, String from, String to) throws UniformInterfaceException {
WebResource resource = webResource;
resource = resource.path(java.text.MessageFormat.format("{0}/{1}", new Object[]{from, to}));
return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML).get(responseType);
}
public <T> T findRange_JSON(Class<T> responseType, String from, String to) throws UniformInterfaceException {
WebResource resource = webResource;
resource = resource.path(java.text.MessageFormat.format("{0}/{1}", new Object[]{from, to}));
return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
}
public <T> T find_XML(Class<T> responseType, String id) throws UniformInterfaceException {
WebResource resource = webResource;
resource = resource.path(java.text.MessageFormat.format("{0}", new Object[]{id}));
return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML).get(responseType);
}
public <T> T find_JSON(Class<T> responseType, String id) throws UniformInterfaceException {
WebResource resource = webResource;
resource = resource.path(java.text.MessageFormat.format("{0}", new Object[]{id}));
return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
}
public void close() {
client.destroy();
}
}
我尝试使用以下内容访问它,并以与XML相同的方式处理它,
ClientResponse response = client.findAll_JSON(ClientResponse.class);
但它给了我
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
如果你能帮我解决这个问题,我将不胜感激。谢谢!
答案 0 :(得分:5)
不要忘记添加以下代码:
DefaultClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
Client client = Client.create(clientConfig);
然后是代码:
return resource.type(MediaType.APPLICATION_JSON_TYPE).get(new GenericType<List<MyClass>>(){});
没问题。
答案 1 :(得分:2)
1)产生此错误的人显然期望XML输入。不是JSON。你需要尽快改变:
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)屏幕截图中的东西(大概是泽西岛?)绝对可以。
3)我没有按照整个教程,你没有提供足够的信息来告诉你哪里误入歧途。
建议:
回顾教程中的步骤,并确保确定您选择的是“JSON”(不是 XML,而不是 SOAP)每一步。
=========== ADDENDUM ===========
确定 - 用于更新的Thanx。这就是我们所处的位置:
1)这就是问题所在:
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)你说这个堆栈追溯来自客户端。
因此,您的服务器100%正常 - 您需要做的 ONLY 事情就是修复您的客户端。酷:)
3)回溯显示客户端正在期待XML ...而是获得JSON。
因此,您需要修复的 ONLY 是告诉您的客户“嘿:读取JSON,而不是XML”。再次 - 酷:)
4)你是怎么做到的?
嗯,对于初学者来说,你需要摆脱这一行(如果你还没有):
// Bad, bad bad. Don't do this!|
ClientResponse response = client.findAll_XML(ClientResponse.class);
5)您可能想要更改客户端代码的其他部分 - 我不知道。
您可能还想更改客户端的配置 - 我也不知道。
6)建议:看看这个其他教程 - 它可能会指出你正确的方向:
注:
你需要做WHATEVER - 它应该真的简单!请查看链接,查看您的代码和测试客户端配置...并回发您找到的内容!
提前谢谢你......
答案 2 :(得分:0)
http://smoothexample.com/webservices/apache_cxf_rest_web_services_client.html
上面的例子给出了使用apache cxf的示例客户端应用程序,这里客户端可以通过提供&#34; Accept&#34;来消费xml和json。报头中。
这里也提供了一个简单的cxf rest web服务示例,它甚至可以根据&#34; Accept&#34;返回xml和json。报头中。