将POJO通过提供程序序列化为JSON。

huangapple go评论85阅读模式
英文:

Serialize POJO to JSON through a provider

问题

什么是一种常见的自动方式将Java Beans 序列化为 JSON?@Produces(MediaType.APPLICATION_JSON) 似乎在底层不处理转换。我们是要创建自己的 JSON 提供程序,还是有其他方法?目前,我正在尝试通过 ContextResolver 处理它:

https://docs.oracle.com/javaee/7/api/javax/ws/rs/ext/ContextResolver.html

我理解 @Produces 注解应该在资源类方法之一返回Java对象时帮助创建预期的输出。

英文:

What is a common way to serialize Java Beans to JSON in a automatic fashion? The @Produces(MediaType.APPLICATION_JSON) doesnt seem to handle the conversion under the hood. Do we create our own JSON Providers or is there another way? Currently im trying to handle it via the ContextResolver:

https://docs.oracle.com/javaee/7/api/javax/ws/rs/ext/ContextResolver.html

It is my understanding that the @Produces annotation should help create the expected output if a java object is returned from one of the resource class methods.

答案1

得分: 0

你不必自己编写基于Jackson的提供程序。

在Apache Karaf中,您可以简单地安装特性 aries-jax-rs-whiteboard-jackson
如果您不使用Karaf,可以自行安装来自特性的捆绑项:

https://github.com/apache/aries-jax-rs-whiteboard/blob/master/jax-rs.features/src/main/feature/feature.xml

英文:

You do not have to code the jackson based provider yourself.

In apache karaf you can simply install the feature aries-jax-rs-whiteboard-jackson.
If you are not using karaf you can install the bundles from the feature yourself:

https://github.com/apache/aries-jax-rs-whiteboard/blob/master/jax-rs.features/src/main/feature/feature.xml

huangapple
  • 本文由 发表于 2020年7月31日 16:14:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63188178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定