java 如何在 Wildfly 中配置 Jackson?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28307646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure Hymanson in Wildfly?
提问by sebplorenz
I've got a Session Bean with the following method:
我有一个具有以下方法的会话 Bean:
@POST
@Consumes("application/x-www-form-urlencoded")
@Path("/calculate")
@Produces("application/json")
public CalculationResult calculate(@FormParam("childProfile") String childProfile,
@FormParam("parentProfile") String parentProfile) {
...
}
The returned CalculationResult cannot be mapped to JSON and the following exception occurs:
返回的CalculationResult无法映射到JSON,出现如下异常:
Caused by: com.fasterxml.Hymanson.databind.JsonMappingException: No serializer found for class com.test.UniqueName and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)...
How can I configure Hymanson and its SerializationFeature
in Wildfly?
如何SerializationFeature
在 Wildfly 中配置 Hymanson 和它?
回答by Paul Samsotha
"How can I configure Hymanson and its SerializationFeature in Wildfly?"
“如何在 Wildfly 中配置 Hymanson 及其 SerializationFeature?”
You don't need to configure it in Wildfly, you can configure it in the JAX-RS applciation. Just use a ContextResolver
to configure the ObjectMapper
(see more here). Something like
您不需要在 Wildfly 中配置它,您可以在 JAX-RS 应用程序中配置它。只需使用 aContextResolver
来配置ObjectMapper
(在此处查看更多信息)。就像是
@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {
private final ObjectMapper mapper;
public ObjectMapperContextResolver() {
mapper = new ObjectMapper();
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
}
@Override
public ObjectMapper getContext(Class<?> type) {
return mapper;
}
}
If you don't already have the Hymanson dependency, you need that, just as a compile-time dependency
如果您还没有 Hymanson 依赖项,则需要它,就像编译时依赖项一样
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-Hymanson-provider</artifactId>
<version>3.0.8.Final</version>
<scope>provided</scope>
</dependency>
If you are using scanning to discover your resource classes and provider classes, the ContextResolver
should be discovered automatically. If you explicitly registering all your resource and providers, then you'll need to register this one also. It should be registered as a singleton.
如果您使用扫描来发现您的资源类和提供程序类,则ContextResolver
应该自动发现它们。如果您明确注册了所有资源和提供者,那么您还需要注册这一点。它应该被注册为单例。
UPDATE
更新
As @KozProv mentions in a comment, it should actually be resteasy-Hymanson2-provider
as the artifactId for the Maven dependency. -Hymanson-
uses the older org.codehaus
(Hymanson 1.x), while the -Hymanson2-
uses the new com.fasterxml
(Hymanson 2.x). Wildfly by default uses The Hymanson 2 version.
正如@KozProv 在评论中提到的,它实际上应该resteasy-Hymanson2-provider
作为 Maven 依赖项的 artifactId。-Hymanson-
使用旧的org.codehaus
(Hymanson 1.x),而-Hymanson2-
使用新的com.fasterxml
(Hymanson 2.x)。Wildfly 默认使用 The Hymanson 2 版本。
回答by Igor Vukovi?
Wildfly 9
野蝇9
pom.xml
pom.xml
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-Hymanson2-provider</artifactId>
<version>3.0.8.Final</version>
<scope>provided</scope>
</dependency>
Java class
Java类
@com.fasterxml.Hymanson.annotation.JsonIgnoreProperties(ignoreUnknown = true)
public class SomePojo implements Serializable {
}