Jersey中的URI扩展名(.xml,.json)

时间:2016-09-06 12:35:24

标签: java jersey

我正在尝试使用Jersey创建一个通用的REST API,我按照以下博客进行了此操作: https://theza.ch/2009/08/11/uri-extensions-in-jersey/

所以正在发生的事情是,当我在我的url中使用.xml时,服务器工作正常,当我使用.json时,它会产生500内部服务器错误。我尝试了不同的东西,但无济于事。任何人都可以通过任何改变知道为什么这会发生在json而不是xml以及如何解决这个问题?

我的代码看起来像这样:

@GET
@Path("/order/{product-key}/getorderid")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response getOrderIdByDomain(@Context HttpServletRequest request,
                                   @PathParam("product-key") final String productKey,
                                   @QueryParam("domain-name") final String domainName ) throws Exception
{
    try
    {
        Integer response = doSomething();

        return Response.status(200).entity(response).build();
    }
    catch (Exception lbe)
    {
        Hashtable response = new Hashtable();
        response.put("Error",lbe.getMessage());
        return Response.status(400).entity(response).build();
    }
}

更新:

添加jersey-json依赖项后,500错误更改为200 OK但我仍然得到一个空响应。对于xml,我得到了预期的响应。有人面临类似的问题吗?请提出一些建议,因为我已尝试过其他答案中的一些内容,但它似乎并没有起作用。

堆栈跟踪:

Caused by: java.lang.AbstractMethodError
at org.codehaus.jackson.map.AnnotationIntrospector$Pair.findSerializer(AnnotationIntrospector.java:1148)
at org.codehaus.jackson.map.ser.BasicSerializerFactory.findSerializerFromAnnotation(BasicSerializerFactory.java:362)
at org.codehaus.jackson.map.ser.BeanSerializerFactory.createSerializer(BeanSerializerFactory.java:252)
at org.codehaus.jackson.map.ser.StdSerializerProvider._createUntypedSerializer(StdSerializerProvider.java:782)
at org.codehaus.jackson.map.ser.StdSerializerProvider._createAndCacheUntypedSerializer(StdSerializerProvider.java:735)
at org.codehaus.jackson.map.ser.StdSerializerProvider.findValueSerializer(StdSerializerProvider.java:344)
at org.codehaus.jackson.map.ser.StdSerializerProvider.findTypedValueSerializer(StdSerializerProvider.java:420)
at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:601)
at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:256)
at org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1606)
at org.codehaus.jackson.jaxrs.JacksonJsonProvider.writeTo(JacksonJsonProvider.java:520)
at com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy.writeTo(JacksonProviderProxy.java:160)
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1437)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
... 48 more

我使用以下依赖项:jersey-server,jersey-json 1.8版。

2 个答案:

答案 0 :(得分:2)

经过长时间的努力解决这个问题后,我转投了genson,

        <dependency>
            <groupId>com.owlike</groupId>
            <artifactId>genson</artifactId>
            <version>1.3</version>
        </dependency>

它非常容易。稍后会试着找出杰克逊为什么不工作。

答案 1 :(得分:0)

在评论中建议添加jar依赖项后,
 你需要有额外的参数来决定名为'format'的xml或json,所以现在你可以通过改变return语句来做到这一点

@GET
    @Path("/order/{product-key}/{format}/getorderid")
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Response getOrderIdByDomain(@Context HttpServletRequest request,
                                       @PathParam("product-key") final String productKey,@PathParam("format") final String format,
                                       @QueryParam("domain-name") final String domainName ) throws Exception
    {
        try
        {
            Integer response = doSomething();

           return Response
                // Set the status and Put your entity here.
                .ok(entity)
                // Add the Content-Type header to tell Jersey which format it should marshall the entity into.
                .header(HttpHeaders.CONTENT_TYPE, "json".equals(format) ? MediaType.APPLICATION_JSON : MediaType.APPLICATION_XML)
                .build();
        }
        catch (Exception lbe)
        {
            Hashtable response = new Hashtable();
            response.put("Error",lbe.getMessage());
            return Response.status(400).entity(response).build();
        }
    }