我正在使用 REST (Jersey 1.8) 开发 Web 服务。目前我正在使用 XML 在 Java 客户端和服务器之间进行通信。
我需要将其更改为 JSON:我该怎么做?我有一堆从 NetBeans 自动生成的代码,不知道该做什么以及如何做。在测试服务时,它会显示 JSON 数据。我无法做的是用我的main
方法处理它。
这些是我遵循的教程
- http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/RESTfulWebServices/RESTfulWebservices.htm
- http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/RESTfulWebServices_Part2/RESTfulWebservicesPart2.htm
- http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/RESTfulWebServices_Part3/RESTfulWebservicesPart3.htm
我的 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();
}
}
人新泽西州
/*
* 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
如果您能在这件事上帮助我,我将不胜感激。谢谢!