我已经被这个问题困扰了好几天,现在正在寻找一些指导来帮助解决这个问题。我在 Metro 下使用 jax-ws 有相当多的经验,但这是我第一次使用 Jersey 进入 jax-rs。为了简化移动部分,我以 jersey-examples-moxy 代码为起点。
我修改了示例项目以同时接受 XML 和 JSON。我认为这很简单,但我似乎错过了一些东西,因为这是一次痛苦的经历。当我请求“application/xml”时,代码运行没有问题,GET 将适用于“application/json”,但是当我尝试使用 JSON 进行 PUT 时,服务器返回 500 状态代码。我可以看到在 PUT 请求中发送了 JSON,但服务器似乎在接受 JSON 时遇到了问题。
这是修改后的 CustomerResource.java 文件段。我所做的只是将 MediaType.APPLICATION_JSON 参数添加到 Produces 和 Consumes 注释中。
@Path("/customer")
public class CustomerResource {
private static Customer customer = createInitialCustomer();
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Customer getCustomer() {
return customer;
}
@PUT
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public void setCustomer(Customer c) {
customer = c;
}
private static Customer createInitialCustomer() {
Customer result = new Customer();
result.setName("Jane Doe");
result.setAddress(new Address("123 Any Street", "My Town"));
result.getPhoneNumbers().add(new PhoneNumber("work", "613-555-1111"));
result.getPhoneNumbers().add(new PhoneNumber("cell", "613-555-2222"));
return result;
}
}
我修改了 MoxyAppTest.java 文件以在单独的测试中请求 XML 和 JSON MediaTypes:
@Test
public void testJaxBCustomer() throws Exception {
final WebTarget webTarget = target().path("customer");
Customer customer = webTarget.request(MediaType.APPLICATION_XML).get(Customer.class);
assertEquals("Jane Doe", customer.getName());
customer.setName("Tom Dooley");
Response response = webTarget.request(MediaType.APPLICATION_XML).put(Entity.xml(customer));
assertEquals(204, response.getStatus());
Customer updatedCustomer = webTarget.request(MediaType.APPLICATION_XML).get(Customer.class);
assertEquals(customer, updatedCustomer);
}
@Test
public void testJsonCustomer() throws Exception {
final WebTarget webTarget = target().path("customer");
Customer customer = webTarget.request(MediaType.APPLICATION_JSON).get(Customer.class);
assertEquals("Tom Dooley", customer.getName());
customer.setName("Bobby Boogie");
Response response = webTarget.request(MediaType.APPLICATION_JSON).put(Entity.json(customer));
assertEquals(204, response.getStatus());
Customer updatedCustomer = webTarget.request(MediaType.APPLICATION_JSON).get(Customer.class);
assertEquals(customer, updatedCustomer);
}
在 App.java 文件中,我在 createApp() 方法中添加了 JsonMoxyConfigurationContextResolver 类和 registerInstances() 调用。(注意去掉这个不会改变结果)。
public static ResourceConfig createApp() {
return new ResourceConfig().packages("org.glassfish.jersey.examples.xmlmoxy")
.register(new MoxyXmlFeature())
.registerInstances(new JsonMoxyConfigurationContextResolver());
}
@Provider
final static class JsonMoxyConfigurationContextResolver implements ContextResolver<MoxyJsonConfig> {
@Override
public MoxyJsonConfig getContext(Class<?> objectType) {
final MoxyJsonConfig configuration = new MoxyJsonConfig();
Map<String, String> namespacePrefixMapper = new HashMap<String, String>(1);
namespacePrefixMapper.put("http://www.w3.org/2001/XMLSchema-instance", "xsi");
configuration.setNamespacePrefixMapper(namespacePrefixMapper);
configuration.setNamespaceSeparator(':');
return configuration;
}
}
以下是显示“application/xml”PUT 成功且状态为 204 的日志段:
Aug 23, 2013 1:23:50 AM org.glassfish.jersey.filter.LoggingFilter log
INFO: 3 * LoggingFilter - Request received on thread main
3 > PUT http://localhost:9998/customer
3 > Accept: application/xml
3 > Content-Type: application/xml
<?xml version="1.0" encoding="UTF-8"?>
<customer><personal-info><name>Tom Dooley</name></personal-info><contact-info><address><city>My Town</city><street>123 Any Street</street></address><phone-number type="work">613-555-1111</phone-number><phone-number type="cell">613-555-2222</phone-number></contact-info></customer>
Aug 23, 2013 1:23:50 AM org.glassfish.jersey.filter.LoggingFilter log
INFO: 4 * LoggingFilter - Response received on thread main
4 < 204
4 < Date: Fri, 23 Aug 2013 05:23:50 GMT
现在对于“应用程序/json”日志:
Aug 23, 2013 1:23:51 AM org.glassfish.jersey.filter.LoggingFilter log
INFO: 3 * LoggingFilter - Request received on thread main
3 > PUT http://localhost:9998/customer
3 > Accept: application/json
3 > Content-Type: application/json
{"personal-info":{"name":"Bobby Boogie"},"contact-info":{"address":{"city":"My Town","street":"123 Any Street"},"phone-number":[{"type":"work","value":"613-555-1111"},{"type":"cell","value":"613-555-2222"}]}}
Aug 23, 2013 1:23:51 AM org.glassfish.jersey.filter.LoggingFilter log
INFO: 4 * LoggingFilter - Response received on thread main
4 < 500
4 < Date: Fri, 23 Aug 2013 05:23:50 GMT
4 < Content-Length: 0
4 < Connection: close
如您所见,服务器正在返回状态代码 500。Grizzly 测试容器没有生成捕获任何异常信息的日志,并且在 500 响应中似乎没有返回任何内容。有没有办法获得额外的异常细节?
关于如何进行的任何建议?
谢谢!