PUT使用带有MOXy的Jersey 2.2下的JAXB进行JSON处理

时间:2013-08-23 06:57:29

标签: xml json jaxb jersey moxy

我一直坚持这个问题好几天,现在正在寻找一些帮助解决问题的指导。我在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 MediaType:

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

这是显示&#34; application / xml&#34;的日志段。 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

现在为&#34; application / json&#34;日志:

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响应中没有返回任何内容。有没有办法可以获得额外的异常细节?

有关如何进行的任何建议?

谢谢!

1 个答案:

答案 0 :(得分:1)

实际上导致这个问题的原因是:

  • @XmlPath注释Customer bean和
  • 您正在设置namespacePrefixMapper
  • MoxyJsonConfig

在(联合国)编组JSON期间导致NullPointerException。这已在MOXy的夜间版本(2.5.12.6.1)中修复。您可以从Nightly Builds页面下载。

如果您暂时不想更改MOXy的版本,只需删除此行

即可
configuration.setNamespacePrefixMapper(namespacePrefixMapper);

来自JsonMoxyConfigurationContextResolver你应该没问题。

关于日志记录问题 - 我们知道它并且已经修复了(应该在Jersey 2.3中解决)。