1

我正在使用 Spring 3.2.1 和 Jackson 2。

我有以下 Spring 配置文件:

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean 
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

            <property name="objectMapper" ref="myObjectMapper" />
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
<bean
    name="myObjectMapper"
    class="org.me.reference.util.MyObjectMapper" />

我的自定义 ObjectMapper 如下所示:

public class MyObjectMapper extends ObjectMapper {
    /**
     * A default version UID to use when serializing an instance of this class.
     */
    private static final long serialVersionUID = 1L;

    public MyObjectMapper() {
        setFilters(
            (new SimpleFilterProvider())
                .addFilter(
                    "CustomJacksonFilter",
                    SimpleBeanPropertyFilter.serializeAllExcept("password"));
    }
}

我的域对象如下所示:

@JsonFilter("CustomJacksonFilter")
public class User {
    /**
     * The JSON key for the user's user-name.
     */
    public static final String JSON_KEY_USERNAME = "username";
    /**
     * The JSON key for the user's password.
     */
    public static final String JSON_KEY_PASSWORD = "password";

    /**
     * The user's user-name.
     */
    @JsonProperty(JSON_KEY_USERNAME)
    private final String username;
    /**
     * The user's password.
     */
    @JsonProperty(JSON_KEY_PASSWORD)
    private final String password;

    ...
}

基本上,我想确保在返回用户对象时密码不会自动序列化。我觉得这应该可以工作,但是我收到以下错误:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not resolve BeanPropertyFilter with id 'CustomJacksonFilter'; no FilterProvider configured

我尝试了上述配置的许多变体,但均未成功。我已经使用视图进行了调查,但是我需要注释我不想序列化的一个字段之外的所有内容,这太麻烦了。

非常感谢任何建议。谢谢,

约翰

编辑:

完整的堆栈跟踪:

SEVERE: The server threw an unexpected exception.
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.mongojack.MongoJsonMappingException: Error mapping BSON to POJOs
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:932)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:827)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.me.reference.filter.AuthFilter.doFilter(AuthFilter.java:244)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.me.reference.filter.ExceptionFilter.doFilter(ExceptionFilter.java:94)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
Caused by: org.mongojack.MongoJsonMappingException: Error mapping BSON to POJOs
    at org.mongojack.JacksonDBCollection.convertToDbObject(JacksonDBCollection.java:1643)
    at org.mongojack.JacksonDBCollection.insert(JacksonDBCollection.java:239)
    at org.me.reference.data.mongodb.MongoUserBin.createUser(MongoUserBin.java:86)
    at org.me.reference.request.UserRegistrationRequest.service(UserRegistrationRequest.java:169)
    at org.me.reference.servlet.Version1.handleRequest(Version1.java:1454)
    at org.me.reference.servlet.Version1.registerUser(Version1.java:1117)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920)
    ... 26 more
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not resolve BeanPropertyFilter with id 'MeJacksonFilter'; no FilterProvider configured
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.findFilter(BeanSerializerBase.java:630)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFieldsFiltered(BeanSerializerBase.java:590)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:141)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:120)
    at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:457)
    at org.mongojack.JacksonDBCollection.convertToDbObject(JacksonDBCollection.java:1641)
    ... 44 more
4

1 回答 1

3

您的ObjectMapper实现工作正常并提供了一个FilterProvider,但是查看您的异常的堆栈跟踪,看起来MappingJackson2HttpMessageConverter您注册的不是被调用以进行序列化。因此,您必须使用不同的ObjectMapper.

于 2013-09-13T19:48:33.500 回答