在这两种情况下,如何在Jersey中都获得具有根元素的JSON和XML响应? [英] How to get both JSON and XML response in Jersey having root element in both the cases?

查看:68
本文介绍了在这两种情况下,如何在Jersey中都获得具有根元素的JSON和XML响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望通过使用单个函数来响应,例如:

I want response by using single function like:

@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response getVolume(){
    ...enter code here
    return Response.ok().entity(VolDetail).build();
}

输出应该像:

xml:
<volume>
   <status>available</status>
</volume>

JSON:
{"volume":{"status":"available"}}

其中volume是POJO类.

问题是我没有在JSON中获得根元素.我尝试了JSON对象绑定,但是无法正常工作.

The problem is that I am not getting root element in JSON. I tried JSON object binding but its not working properly.

推荐答案

假设您正在使用Jackson.您可以将ObjectMapper配置为WRAP_ROOT_VALUE.您可以在ContextResolver中执行此操作.使用Jackson 1.x,它看起来像

Assuming you're using Jackson. You can configure the ObjectMapper to WRAP_ROOT_VALUE. You would do that in the ContextResolver. With Jackson 1.x, it would look like

import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig.Feature;

@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper>  {

    final ObjectMapper mapper = new ObjectMapper();

    public ObjectMapperContextResolver() {
        mapper.configure(Feature.WRAP_ROOT_VALUE, true);
    }

    @Override
    public ObjectMapper getContext(Class<?> type) {
        return mapper;
    }  
}

对于Jackson 2.x,它看起来像

With Jackson 2.x, it would look like

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper>  {

    final ObjectMapper mapper = new ObjectMapper();

    public ObjectMapperContextResolver() {
        mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
    }

    @Override
    public ObjectMapper getContext(Class<?> type) {
        return mapper;
    }  
}

您的POJO应使用@XmlRootElement(name = "volume")@JsonRootName("volume")

Your POJO should be annotated with @XmlRootElement(name = "volume") or @JsonRootName("volume")

如果您不希望包装所有对象,则可以为不同的类配置不同的映射器,如在此处查看

If you don't want all your objects wrapped, you can configure different mappers for different classes, as seen here

使用上述解决方案,仅@JsonRootName将起作用.原因是通过使用我们自己的ObjectMapper,我们覆盖了JAXB注释支持配置的行为.我们可以通过mapper.registerModule(new JaxbAnnotationModule());

With the above solution, only @JsonRootName will work. The reason is that by using our own ObjectMapper, we override the behavior of a JAXB annotation support configuration. We could explicitly add the support back by mapper.registerModule(new JaxbAnnotationModule());

这篇关于在这两种情况下,如何在Jersey中都获得具有根元素的JSON和XML响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆