网站模版html,seo有哪些经典的案例,宁波公司建网站哪家好,网站全网建设 莱芜如果我们在Spring Boot应用中手动定义并注入了一个ObjectMapper Bean#xff0c;那么这个自定义的ObjectMapper实例会替换掉Spring Boot默认配置的ObjectMapper。当Spring容器中存在多个同类型的Bean时#xff0c;默认情况下最后一个创建的Bean将作为首选Bean#xff08;如果…如果我们在Spring Boot应用中手动定义并注入了一个ObjectMapper Bean那么这个自定义的ObjectMapper实例会替换掉Spring Boot默认配置的ObjectMapper。当Spring容器中存在多个同类型的Bean时默认情况下最后一个创建的Bean将作为首选Bean如果未明确指定Primary注解因此我们的自定义ObjectMapper将会被所有依赖于ObjectMapper的地方使用。
例如
Configuration
public class ObjectMapperConfig {Beanpublic ObjectMapper customObjectMapper() {ObjectMapper objectMapper new ObjectMapper();// 添加自定义配置...return objectMapper;}
}上述代码定义了一个自定义的ObjectMapper Bean并将其注册到了Spring容器中。这样一来在整个应用中需要ObjectMapper的地方包括HTTP请求和响应的JSON转换等场景都会使用到这个自定义配置的ObjectMapper而非Spring Boot默认提供的那个。
因此如果我们只想修改Spring Boot默认ObjectMapper的一些配置而不是完全替换掉它使用Jackson2ObjectMapperBuilderCustomizer接口是一个更好的选择。通过实现这个接口并注册一个定制器Bean我们可以对默认的ObjectMapper进行扩展和修改而不会覆盖其他默认配置。
下面是一个例子
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
public class JacksonConfig {Beanpublic Jackson2ObjectMapperBuilderCustomizer jacksonCustomizer() {return builder - {// 修改日期格式化builder.dateFormat(yyyy-MM-ddTHH:mm:ss.SSSZ);// 关闭未知属性导致反序列化失败builder.featuresToDisable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);// 其他自定义配置...};}
}这样我们的配置将应用于所有的ObjectMapper实例包括那些由Spring Boot自动配置创建的实例。这意味着在HTTP请求响应处理、消息转换等任何使用到ObjectMapper的地方都会采用我们自定义的配置。
另外Jackson2ObjectMapperBuilderCustomizer接口并不能配置空值序列化操作因此我们可以这样 // 该方式不会完全替换Springboot默认的ObjectMapper并且可以设置空值序列化器BeanPrimaryConditionalOnMissingBean(ObjectMapper.class)public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);builder.modules(getJavaLongSimpleModule(), getJavaTimeSimpleModule());ObjectMapper objectMapper builder.createXmlMapper(false).build();objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializerObject() {Overridepublic void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {jsonGenerator.writeString();}});return objectMapper;}该方式不会完全替换Springboot默认的ObjectMapper并且可以设置空值序列化器。
注JsonSerializer无法在序列化时对空值操作因为其serialize方法接收到的被序列化对象永远不为null
/*** Abstract class that defines API used by {link ObjectMapper} (and* other chained {link JsonSerializer}s too) to serialize Objects of* arbitrary types into JSON, using provided {link JsonGenerator}.* {link com.fasterxml.jackson.databind.ser.std.StdSerializer} instead* of this class, since it will implement many of optional* methods of this class.*p* NOTE: various codeserialize/code methods are never (to be) called* with null values -- caller bmust/b handle null values, usually* by calling {link SerializerProvider#findNullValueSerializer} to obtain* serializer to use.* This also means that custom serializers cannot be directly used to change* the output to produce when serializing null values.*p* If serializer is an aggregate one -- meaning it delegates handling of some* of its contents by using other serializer(s) -- it typically also needs* to implement {link com.fasterxml.jackson.databind.ser.ResolvableSerializer},* which can locate secondary serializers needed. This is important to allow dynamic* overrides of serializers; separate call interface is needed to separate* resolution of secondary serializers (which may have cyclic link back* to serializer itself, directly or indirectly).*p* In addition, to support per-property annotations (to configure aspects* of serialization on per-property basis), serializers may want* to implement * {link com.fasterxml.jackson.databind.ser.ContextualSerializer},* which allows specialization of serializers: call to* {link com.fasterxml.jackson.databind.ser.ContextualSerializer#createContextual}* is passed information on property, and can create a newly configured* serializer for handling that particular property.*p* If both* {link com.fasterxml.jackson.databind.ser.ResolvableSerializer} and* {link com.fasterxml.jackson.databind.ser.ContextualSerializer}* are implemented, resolution of serializers occurs before* contextualization.*/public abstract class JsonSerializerT implements JsonFormatVisitable // since 2.1{/*** Method that can be called to ask implementation to serialize* values of type this serializer handles.** param value Value to serialize; can bnot/b be null.* param gen Generator used to output resulting Json content* param serializers Provider that can be used to get serializers for* serializing Objects value contains, if any.*/public abstract void serialize(T value, JsonGenerator gen, SerializerProvider serializers)throws IOException;}