JAX-RS:在 payara micro 上序列化 POJO 失败
JAX-RS: serializing a POJO fails on payara micro
将我们的应用程序 war 从 Glassfish3 移动到使用 Payara Micro 的部署后,JAX-RS 序列化(jersey + jackson)不再起作用。
感谢Adam,我们解决了序列化纯集合的问题,现在我们在返回 POJO 时遇到类似的错误:
@GET
@Produces("application/json")
public BirdyTO findAllDaBirdy() {
return getBirdy();
}
其中 BirdyTO 是一个 POJO,它包含其他 POJOS and/or 个 POJOS 集合。
那个给我们的错误:
MessageBodyWriter not found for media type=application/json;charset=utf-8, type=class org.example.BirdyTO, genericType=class org.example.BirdyTO.
奇怪的是,同一个应用程序中的相似接口工作正常。
有什么想法吗?
POJO 到 JSON 的映射在 Java EE 中未标准化。 Glassfih 4/Payara 默认使用 MOXy 将 POJO 映射到 JSON,它使用 JAXB 进行映射。参见 [Reza Rahman 的 post] (https://blogs.oracle.com/theaquarium/entry/moxy_is_the_new_default)。 BirdyTO 可能无法被 Moxy 映射。
如果你想使用 Jackson,你必须:
- 禁用默认的 Moxy 功能(通过将
jersey.config.server.disableMoxyJson
属性 设置为 true
)
- 将 Jackson 库添加到您的应用中 (
com.fasterxml.jackson.jaxrs
)
- 在您的 JAX-RS 应用程序中打开 JacksonFeature(由 Jackson 图书馆提供)
更多信息如何在这个答案中做到这一点:Customizing JSON marhsalling with GlassFish v4
将我们的应用程序 war 从 Glassfish3 移动到使用 Payara Micro 的部署后,JAX-RS 序列化(jersey + jackson)不再起作用。
感谢Adam,我们解决了序列化纯集合的问题,现在我们在返回 POJO 时遇到类似的错误:
@GET
@Produces("application/json")
public BirdyTO findAllDaBirdy() {
return getBirdy();
}
其中 BirdyTO 是一个 POJO,它包含其他 POJOS and/or 个 POJOS 集合。
那个给我们的错误:
MessageBodyWriter not found for media type=application/json;charset=utf-8, type=class org.example.BirdyTO, genericType=class org.example.BirdyTO.
奇怪的是,同一个应用程序中的相似接口工作正常。
有什么想法吗?
POJO 到 JSON 的映射在 Java EE 中未标准化。 Glassfih 4/Payara 默认使用 MOXy 将 POJO 映射到 JSON,它使用 JAXB 进行映射。参见 [Reza Rahman 的 post] (https://blogs.oracle.com/theaquarium/entry/moxy_is_the_new_default)。 BirdyTO 可能无法被 Moxy 映射。
如果你想使用 Jackson,你必须:
- 禁用默认的 Moxy 功能(通过将
jersey.config.server.disableMoxyJson
属性 设置为true
) - 将 Jackson 库添加到您的应用中 (
com.fasterxml.jackson.jaxrs
) - 在您的 JAX-RS 应用程序中打开 JacksonFeature(由 Jackson 图书馆提供)
更多信息如何在这个答案中做到这一点:Customizing JSON marhsalling with GlassFish v4