如何在 Wildfly 中配置 Jackson? [英] How to configure Jackson in Wildfly?

查看:28
本文介绍了如何在 Wildfly 中配置 Jackson?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用以下方法的会话 Bean:

I've got a Session Bean with the following method:

@POST
@Consumes("application/x-www-form-urlencoded")
@Path("/calculate")
@Produces("application/json")
public CalculationResult calculate(@FormParam("childProfile") String childProfile,
        @FormParam("parentProfile") String parentProfile) {
...
}

返回的CalculationResult无法映射到JSON,出现如下异常:

The returned CalculationResult cannot be mapped to JSON and the following exception occurs:

Caused by: com.fasterxml.jackson.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)...

如何在 Wildfly 中配置 Jackson 及其SerializationFeature?

How can I configure Jackson and its SerializationFeature in Wildfly?

推荐答案

如何在 Wildfly 中配置 Jackson 及其序列化功能?"

"How can I configure Jackson and its SerializationFeature in Wildfly?"

您不需要在Wildfly 中配置它,您可以在JAX-RS 应用程序中配置它.只需使用 ContextResolver 配置 ObjectMapper(查看更多此处).类似的东西

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

@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;
    }
    
}

如果您还没有 Jackson 依赖项,则需要它,就像编译时依赖项

If you don't already have the Jackson dependency, you need that, just as a compile-time dependency

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jackson-provider</artifactId>
    <version>3.0.8.Final</version>
    <scope>provided</scope>
</dependency>

如果您使用扫描来发现您的资源类和提供程序类,则应该会自动发现 ContextResolver.如果您明确注册了所有资源和提供者,那么您还需要注册这一点.它应该被注册为单例.

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.

正如@KozProv 在评论中提到的,它实际上应该是 resteasy-jackson2-provider 作为 Maven 依赖项的 artifactId.-jackson- 使用旧的 org.codehaus (Jackson 1.x),而 -jackson2- 使用新的 com.fastxml (Jackson 2.x).Wildfly 默认使用 The Jackson 2 版本.

As @KozProv mentions in a comment, it should actually be resteasy-jackson2-provider as the artifactId for the Maven dependency. -jackson- uses the older org.codehaus (Jackson 1.x), while the -jackson2- uses the new com.fasterxml (Jackson 2.x). Wildfly by default uses The Jackson 2 version.

这篇关于如何在 Wildfly 中配置 Jackson?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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