[英]Jersey non-annotated Provider
我正在將Spring的Jersey與REST API一起使用,並且編寫了一個提供程序來修改JSON序列化。 問題是,當我使用@Component
批注時,將為其他servlet調用提供程序的回調方法。 當我刪除@Component
批注時,根本不會調用它。
這是提供者:
@Component
@Provider
public class ObjectMapperProvider implements ContextResolver<ObjectMapper> {
public ObjectMapperProvider() {
}
@Override
public ObjectMapper getContext(Class<?> type) {
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule("SimpleModule", new org.codehaus.jackson.Version(1, 0, 0, null));
module.addSerializer(BigInteger.class, new ToStringSerializer());
objectMapper.registerModule(module);
return objectMapper;
}
}
我試圖在web.xml中使用Jersey配置,但這也無濟於事。
有任何想法嗎?
顯然,這不是我的問題。 提供者被要求使用正確的servlet。
我的應用程序無法正常工作,因為我有一個用於地圖的XmlAdapter,並且使用ObjectMapperProvider時,json響應有所不同。
這是我更新的ObjectMapperProvider類:
@Provider
public class ObjectMapperProvider implements ContextResolver<ObjectMapper> {
@Context
UriInfo uriInfo;
public ObjectMapperProvider() {
}
@Override
public ObjectMapper getContext(Class<?> type) {
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule("SimpleModule", new org.codehaus.jackson.Version(1, 0, 0, null));
module.addSerializer(BigInteger.class, new ToStringSerializer());
objectMapper = objectMapper.configure(Feature.WRAP_ROOT_VALUE, false).configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, false)
.configure(Feature.WRAP_EXCEPTIONS, true).configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, true).configure(Feature.WRITE_EMPTY_JSON_ARRAYS, false);
final AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
objectMapper.getDeserializationConfig().setAnnotationIntrospector(introspector); // using a deprecated API that works. Non-deprecated API doesn't work...
objectMapper.getSerializationConfig().setAnnotationIntrospector(introspector);
objectMapper.registerModule(module);
return objectMapper;
}
}
一旦將對象包裝器配置為使用JAXB批注,一切都會按預期進行。 我從以下帖子中獲得了有關如何執行此操作的想法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.