使用Apache Wink覆盖Websphere 8.5.5上的Jackson对象映射器属性 [英] Override Jackson Object Mapper properties on Websphere 8.5.5 using Apache Wink

查看:97
本文介绍了使用Apache Wink覆盖Websphere 8.5.5上的Jackson对象映射器属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用IBM捆绑的Apache Wink为我们的应用程序提供JAXRS端点.我们正在针对Websphere 8.5.5进行编码.由于我们符合Servlet 3.0,因此我们使用编程"方式配置JaxRS应用程序,这意味着web.xml中没有条目,并且我们依靠类扫描来获取带注释的jax rs资源.一般来说,它工作正常.

We are using IBM(s) bundled Apache Wink to offer JAXRS endpoints for our application. We are coding towards Websphere 8.5.5. Since we are servlet 3.0 compliant we use the 'programmatic' way of configuring the JaxRS application, meaning no entries in web.xml and we rely on class scanning for annotated jax rs resources. In general it works fine.

   @ApplicationPath("/api/v1/") 
   public class MyApplication  extends Application{

此版本的Websphere和Apache Wink一起使用Jackson 1.6.x进行JSON解串/串行化,并且总体来说效果很好.我们想更改一些对象映射器的默认值

This version of Websphere along with Apache Wink, uses Jackson 1.6.x for JSON de/serialization and in general it works well. We would like though to change some of the default values of the Object Mapper

因此,我们定义了一个客户上下文解析器,只需更改一些se/deserialzation属性即可.

So we have defined a customer context resolver, where just alter some of the se/deserialzation properties.

@Provider
@Produces(MediaType.APPLICATION_JSON)
public class CustomJackssonConverter implements ContextResolver<ObjectMapper> {

    final ObjectMapper defaultObjectMapper;

    public AibasJackssonConverter() {
        defaultObjectMapper = createDefaultMapper();
    }
   ...       
 mapper.getSerializationConfig().set(SerializationConfig.Feature.INDENT_OUTPUT, true);

在JAX-RS调用期间,我们可以看到容器注册了新的Provider,没有错误

During JAX-RS calls we can see that the container registers the new Provider, with no errors

问题是,未从日志中配置",我可以看到Wink引擎正在查找WinkJacksonProvider,该WinkJacksonProvider依次返回遵循Jackson的默认值的JacksonProvider ?

The problem is that , the Configuration is not 'followed', from the logs I can see that the Wink Engine is looking up a WinkJacksonProvider, which in turn..returns a JacksonProvider that is following the Jackson(s) default values?

是否有一种方法可以仅更改此默认值?

Is there a way to just change this default value?

我已尝试按照此处指示的那样更改Application对象的实现,以便以编程方式配置Provider,但无法正常工作.

I have tried to change the implementation of the Application object as indicated here, in order to configure Providers programmatically, but it did not work.

http://www.ibm.com/developerworks/java/library /wa-aj-jackson/index.html

有任何提示或提示吗?

非常感谢

推荐答案

我仅通过实现MessageBodyWriter类来解决此问题,如下所示:

I solved this problem by just implementing a MessageBodyWriter class, like this:

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;

@Provider
@Produces(MediaType.APPLICATION_JSON)
public class DefaultMessageBodyWriter implements MessageBodyWriter<Object> {

    @Override
    public long getSize(Object object, 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 object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
        mapper.writeValue(entityStream, object);
    }
}

每次请求JSON序列化时,此类都会起作用,并最终调用其writeTo方法.

Every time a JSON serialization is requested, this class comes into action and finally its writeTo method is invoked.

应WebSphere的要求,在此处关闭SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS.

Here SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS is turned off, as requested by WebSphere.

这篇关于使用Apache Wink覆盖Websphere 8.5.5上的Jackson对象映射器属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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