如何自定义 JAXB 对象列表到 JSON 的序列化?

发布于 2024-08-20 05:33:52 字数 3225 浏览 1 评论 0原文

我正在使用 Jersey 为服务器组件创建 REST Web 服务。

我想要在列表中序列化的 JAXB 注释对象如下所示:

@XmlRootElement(name = "distribution")
@XmlType(name = "tDistribution", propOrder = {
    "id", "name"
})
public class XMLDistribution {
    private String id;
    private String name;
    // no-args constructor, getters, setters, etc
}

我有一个 REST 资源来检索一个发行版,如下所示:

@Path("/distribution/{id: [1-9][0-9]*}")
public class RESTDistribution {
    @GET
    @Produces("application/json")
    public XMLDistribution retrieve(@PathParam("id") String id) {
        return retrieveDistribution(Long.parseLong(id));
    }
    // business logic (retrieveDistribution(long))
}

我还有一个 REST 资源来检索所有发行版的列表,如下所示:

@Path("/distributions")
public class RESTDistributions {
    @GET
    @Produces("application/json")
    public List<XMLDistribution> retrieveAll() {
        return retrieveDistributions();
    }
    // business logic (retrieveDistributions())
}

I使用 ContextResolver 自定义 JAXB 序列化,当前配置如下:

@Provider
@Produces("application/json")
public class JAXBJSONContextResolver implements ContextResolver<JAXBContext> {
    private JAXBContext context;
    public JAXBJSONContextResolver() throws Exception {
        JSONConfiguration.MappedBuilder b = JSONConfiguration.mapped();
        b.nonStrings("id");
        b.rootUnwrapping(true);
        b.arrays("distribution");
        context = new JSONJAXBContext(b.build(), XMLDistribution.class);
    }
    @Override
    public JAXBContext getContext(Class<?> objectType) {
        return context;
    }
}

REST 资源和上下文解析器都可以工作。这是第一个输出的示例:

// path: /distribution/1
{
  "id": 1,
  "name": "Example Distribution"
}

这正是我想要的。这是列表输出的示例:

// path: /distributions
{
  "distribution": [{
    "id": 1,
    "name": "Sample Distribution 1"
  }, {
    "id": 2,
    "name": "Sample Distribution 2"
  }]
}

这并不完全是我想要的。

我不明白为什么那里有一个封闭的 distribution 标签。我想在上下文解析器中使用 .rootUnwrapping(true) 删除它,但显​​然这只删除了另一个封闭的标签。这是 .rootUnwrapping(false) 的输出:

// path: /distribution/1
{
  "distribution": {
    "id": 1,
    "name": "Example Distribution"
  }
} // not ok
// path: /distributions
{
  "xMLDistributions": {
    "distribution": [{
      "id": 1,
      "name": "Sample Distribution 1"
    }, {
      "id": 2,
      "name": "Sample Distribution 2"
    }]
  }
}

我还必须配置 .arrays("distribution") 以始终获取 JSON 数组,即使只有一个元素也是如此。

理想情况下,我希望将其作为输出:

// path: /distribution/1
{
  "id": 1,
  "name": "Example Distribution"
} // currently works
// path: /distributions
[{
  "id": 1,
  "name": "Sample Distribution 1"
}, {
  "id": 2,
  "name": "Sample Distribution 2"
}]

我尝试返回一个 List、一个 XMLDistributionList (列表的包装器)、一个 XMLDistribution[],但我找不到一种方法来获取所需格式的简单 JSON 分布数组。

我还尝试了 JSONConfiguration.natural()JSONConfiguration.mappedJettison() 等返回的其他符号,但无法得到与我需要的类似的东西。

有谁知道是否可以配置 JAXB 来执行此操作?

I'm using Jersey to create a REST web service for a server component.

The JAXB-annotated object I want to serialize in a list looks like this:

@XmlRootElement(name = "distribution")
@XmlType(name = "tDistribution", propOrder = {
    "id", "name"
})
public class XMLDistribution {
    private String id;
    private String name;
    // no-args constructor, getters, setters, etc
}

I have a REST resource to retrieve one distribution which looks like this:

@Path("/distribution/{id: [1-9][0-9]*}")
public class RESTDistribution {
    @GET
    @Produces("application/json")
    public XMLDistribution retrieve(@PathParam("id") String id) {
        return retrieveDistribution(Long.parseLong(id));
    }
    // business logic (retrieveDistribution(long))
}

I also have a REST resource to retrieve a list of all distributions, which looks like this:

@Path("/distributions")
public class RESTDistributions {
    @GET
    @Produces("application/json")
    public List<XMLDistribution> retrieveAll() {
        return retrieveDistributions();
    }
    // business logic (retrieveDistributions())
}

I use a ContextResolver to customize JAXB serialization, which is currently configured like this:

@Provider
@Produces("application/json")
public class JAXBJSONContextResolver implements ContextResolver<JAXBContext> {
    private JAXBContext context;
    public JAXBJSONContextResolver() throws Exception {
        JSONConfiguration.MappedBuilder b = JSONConfiguration.mapped();
        b.nonStrings("id");
        b.rootUnwrapping(true);
        b.arrays("distribution");
        context = new JSONJAXBContext(b.build(), XMLDistribution.class);
    }
    @Override
    public JAXBContext getContext(Class<?> objectType) {
        return context;
    }
}

Both REST resources work, as well as the context resolver. This is an example of output for the first one:

// path: /distribution/1
{
  "id": 1,
  "name": "Example Distribution"
}

Which is exactly what I want. This is an example of output for the list:

// path: /distributions
{
  "distribution": [{
    "id": 1,
    "name": "Sample Distribution 1"
  }, {
    "id": 2,
    "name": "Sample Distribution 2"
  }]
}

Which is not quite what I want.

I don't understand why there is an enclosing distribution tag there. I wanted to remove it with .rootUnwrapping(true) in the context resolver, but apparently that only removes another enclosing tag. This is the output with .rootUnwrapping(false):

// path: /distribution/1
{
  "distribution": {
    "id": 1,
    "name": "Example Distribution"
  }
} // not ok
// path: /distributions
{
  "xMLDistributions": {
    "distribution": [{
      "id": 1,
      "name": "Sample Distribution 1"
    }, {
      "id": 2,
      "name": "Sample Distribution 2"
    }]
  }
}

I also had to configure .arrays("distribution") to always get a JSON array, even with only one element.

Ideally, I'd like to have this as an output:

// path: /distribution/1
{
  "id": 1,
  "name": "Example Distribution"
} // currently works
// path: /distributions
[{
  "id": 1,
  "name": "Sample Distribution 1"
}, {
  "id": 2,
  "name": "Sample Distribution 2"
}]

I tried to return a List<XMLDistribution>, a XMLDistributionList (wrapper around a list), a XMLDistribution[], but I couldn't find a way to get a simple JSON array of distributions in my required format.

I also tried the other notations returned by JSONConfiguration.natural(), JSONConfiguration.mappedJettison(), etc, and couldn't get anything resembling what I need.

Does anyone know if it is possible to configure JAXB to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡離愁欲言轉身 2024-08-27 05:33:53

我找到了一个解决方案:用 Jackson 等性能更好的 JSON 序列化器替换 JAXB JSON 序列化器。最简单的方法是使用 jackson-jaxrs,它已经为您完成了。该类是 JacksonJsonProvider。您所要做的就是编辑项目的 web.xml,以便 Jersey(或其他 JAX-RS 实现)对其进行扫描。以下是您需要添加的内容:

<init-param>
  <param-name>com.sun.jersey.config.property.packages</param-name>
  <param-value>your.project.packages;org.codehaus.jackson.jaxrs</param-value>
</init-param>

这就是全部内容。 Jackson 将用于 JSON 序列化,它的工作方式与您期望的列表和数组一样。

更长的方法是编写您自己的自定义 MessageBodyWriter 注册以生成“application/json”。这是一个示例:

@Provider
@Produces("application/json")
public class JsonMessageBodyWriter implements MessageBodyWriter {
    @Override
    public long getSize(Object obj, Class type, Type genericType,
            Annotation[] annotations, MediaType mediaType) {
        return -1;
    }

    @Override
    public boolean isWriteable(Class type, Type genericType,
            Annotation annotations[], MediaType mediaType) {
        return true;
    }

    @Override
    public void writeTo(Object target, Class type, Type genericType,
            Annotation[] annotations, MediaType mediaType,
            MultivaluedMap httpHeaders, OutputStream outputStream)
            throws IOException {        
        new ObjectMapper().writeValue(outputStream, target);
    }
}

您需要确保您的 web.xml 包含该包,就像上面的现成解决方案一样。

不管怎样:瞧!您将看到格式正确的 JSON。

您可以从这里下载杰克逊:
http://jackson.codehaus.org/

I found a solution: replace the JAXB JSON serializer with a better behaved JSON serializer like Jackson. The easy way is to use jackson-jaxrs, which has already done it for you. The class is JacksonJsonProvider. All you have to do is edit your project's web.xml so that Jersey (or another JAX-RS implementation) scans for it. Here's what you need to add:

<init-param>
  <param-name>com.sun.jersey.config.property.packages</param-name>
  <param-value>your.project.packages;org.codehaus.jackson.jaxrs</param-value>
</init-param>

And that's all there is to it. Jackson will be used for JSON serialization, and it works the way you expect for lists and arrays.

The longer way is to write your own custom MessageBodyWriter registered to produce "application/json". Here's an example:

@Provider
@Produces("application/json")
public class JsonMessageBodyWriter implements MessageBodyWriter {
    @Override
    public long getSize(Object obj, Class type, Type genericType,
            Annotation[] annotations, MediaType mediaType) {
        return -1;
    }

    @Override
    public boolean isWriteable(Class type, Type genericType,
            Annotation annotations[], MediaType mediaType) {
        return true;
    }

    @Override
    public void writeTo(Object target, Class type, Type genericType,
            Annotation[] annotations, MediaType mediaType,
            MultivaluedMap httpHeaders, OutputStream outputStream)
            throws IOException {        
        new ObjectMapper().writeValue(outputStream, target);
    }
}

You'll need to make sure your web.xml includes the package, as for the ready-made solution above.

Either way: voila! You'll see properly formed JSON.

You can download Jackson from here:
http://jackson.codehaus.org/

尘曦 2024-08-27 05:33:53

Jonhatan 的回答非常棒,对我来说非常有用。

只是升级:

如果您使用 Jackson 的 2.x 版本(例如版本 2.1),则该类为 com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider,因此 web.xml 为:

<init-param>
  <param-name>com.sun.jersey.config.property.packages</param-name>
  <param-value>your.project.packages;com.fasterxml.jackson.jaxrs.json</param-value>
</init-param>

The answer of Jonhatan is great and it has been very useful for me.

Just an upgrade:

if you use the version 2.x of Jackson (e.g. version 2.1) the class is com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider, therefore the web.xml is:

<init-param>
  <param-name>com.sun.jersey.config.property.packages</param-name>
  <param-value>your.project.packages;com.fasterxml.jackson.jaxrs.json</param-value>
</init-param>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文