spring mvc 使用jackson暴露http+json服务时,如何设置jackson

1.自定义ObjectMapper
public class CustomObjectMapper extends ObjectMapper
{
    public CustomObjectMapper()
    {
        super();
        configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        setVisibilityChecker(getSerializationConfig()
            .getDefaultVisibilityChecker()
            .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
            .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
        configure(MapperFeature.REQUIRE_SETTERS_FOR_GETTERS, true);
        configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
}

2.org.springframework.http.converter.json.MappingJackson2HttpMessageConverter中注入objectMapper

<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="package.path.CustomObjectMapper" />
</property>
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>

你可能感兴趣的:(spring,mvc,json,Jackson)