2013-09-29 67 views
0

我對Jersey使用Spring和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,但沒有幫助。
任何想法?

回答

0

顯然,那不是我的問題。提供者被稱爲正確的servlet。
我的應用程序沒有工作,因爲我有一個Map的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註釋,一切都按預期。我對如何做到這一點有如下想法post