JAX-RS 2.0:如何注册Feature或ContextResolver以在Mapper中注册自定义JaxbAnnotationIntrospector内省 [英] JAX-RS 2.0: How to register a Feature or a ContextResolver to have a custom JaxbAnnotationIntrospector introspection registered in the Mapper

查看:576
本文介绍了JAX-RS 2.0:如何注册Feature或ContextResolver以在Mapper中注册自定义JaxbAnnotationIntrospector内省的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的JAX-RS 2.0应用程序中正确注册了JacksonJaxbJsonProvider:

I have correctly registered JacksonJaxbJsonProvider in my JAX-RS 2.0 application using:

    public Set<Class<?>> getClasses() {
... classes.add(com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider.class);
}

现在我需要自定义它在JAX-RS资源中自己使用的Mapper因为我需要添加SerializationFeature.FAIL_ON_EMPTY_BEANS。

Now I need to customise the Mapper it self used in the JAX-RS resources because I need to add SerializationFeature.FAIL_ON_EMPTY_BEANS.

我已经看到了如何使用功能或自定义Contextresolver执行此操作的示例,但我知道如何将其添加到Mapper我在我的源代码中创建但我不能将它添加到REST服务使用的Mapper中:

I have seen examples of how to do this using a Feature or a custom Contextresolver, but I know how to add it to a Mapper I create in my source code but I cannot add it to the Mapper that the REST services use using:

classes.add(ObjectMapperContextResolver.class);

classes.add(MarshallingFeature.class);

因为JAX-RS 2.0应用程序忽略了这些类。

Because the JAX-RS 2.0 app ignores those classes.

如何在我的JAX-RS应用程序中添加Feature或ContextResolver以便识别它们?

How can I add the Feature or ContextResolver in my JAX-RS application so they are recognised?

推荐答案

你可以重用Jersey实现自定义应用程序。 更多信息使用ContextResolver获取要使用的自定义ObjectMapper。

You could reuse the Jersey implementation custom application. More Info And using ContextResolver get the custom ObjectMapper to use.

示例:

public class MyApplication extends ResourceConfig {
   public MyApplication() {
      /* packages that contains JAX-RS components, registers them to use also */
      packages("org.foo.rest;org.bar.rest");
      /* here register the provider */
      register(MapperProvider.class);
 }

}

MapperProvider类。

The MapperProvider Class.

@Provider
public class MapperProvider implements ContextResolver<ObjectMapper> {

@Override
public ObjectMapper getContext(Class<?> arg0) {
   /* you can configure the mapper as you like */
   return new ObjectMapper()
            .registerModule(new JaxbAnnotationModule()
            .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false));
}

这篇关于JAX-RS 2.0:如何注册Feature或ContextResolver以在Mapper中注册自定义JaxbAnnotationIntrospector内省的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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