_locateMapperViaProvider 在使用 Aries Felix、Aries Blueprint 和 CXF-RS 时是否有效?

Does _locateMapperViaProvider work when using Aries Felix, Aries Blueprint, and CXF-RS?

为什么@Provider 注释不起作用? (目标是将模块附加到 ObjectMapper 单例)

根据JavaDocs

Default constructor, usually used when provider is automatically configured to be used with JAX-RS implementation.

使用空构造函数调用 JacksonJsonProvider 时,

public JacksonJsonProvider() {
        this((ObjectMapper)null, BASIC_ANNOTATIONS);
    }

ObjectMapper 刚刚设置为空。 BASIC_ANNOTATIONS 可能包含 @Provider.

在 Blueprint 将 classes 初始化到内存后,一定可以设置 ObjectMapper。

所以我有两个选择:

看起来像

protected ObjectMapper _locateMapperViaProvider

蓝图

<cxf:rsServer id="cxfConsumer" address="/" serviceClass="com.domain.MyServiceInterface">
     <cxf:providers>
         <ref component-id="jsonProvider"/>
    </cxf:providers>
 </cxf:rsServer>
 <bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider">

我的@Provider class

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

    final ObjectMapper defaultObjectMapper;

    public JacksonObjectMapperProvider() {
        this.defaultObjectMapper = makeObjectMapper();
    }

    @Override
    public ObjectMapper getContext(Class<?> type) {
        return defaultObjectMapper;
    }

    private static ObjectMapper makeObjectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new MyObjectSerializerModule());
        return mapper;
    }
}

我的序列化器

public class MyObjectSerializerModule extends SimpleModule {

    public MyObjectSerializerModule() {
        super("ObjectIdSerializerModule", new Version(1, 0, 0, "alpha"));
        this.addSerializer(MyObject.class, new MyObjectSerializer());
    }

    public class MyObjectSerializer extends JsonSerializer<Location> {

        @Override
        public void serialize(MyObject value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
                throws IOException {

            jsonGenerator.writeStartObject();
            jsonGenerator.writeStringField("id", value.getId());
            jsonGenerator.writeEndObject();
        }
    }
}

看来答案是,"No sir"。

据我所知,@Provider 注释不起作用。在初始化期间,供应商必须 injected/registered 与 CXF。我欢迎其他答案。

<cxf:rsServer id="cxfConsumer" address="/" serviceClass="com.domain.MyServiceInterface">
     <cxf:providers>
         <ref component-id="jsonProvider"/>
         <ref component-id="JacksonObjectMapperContextProvider"/>
    </cxf:providers>
 </cxf:rsServer>
 <bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider">
<bean id="jsonProvider" class="JacksonObjectMapperContextProvider">