java 如何在全局范围内使用 spring 配置 jackson?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37492249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:33:20  来源:igfitidea点击:

How to configure Hymanson with spring globally?

javaspringHymanson

提问by Nabil Sham

To serialize deserialize object I am useing Hymanson as flow

为了序列化反序列化对象,我使用 Hymanson 作为流程

@JsonSerialize(using = LocalDateSerializer.class)
@JsonDeserialize(using = LocalDateDeserializer.class)
private LocalDate openingDate 

How do I make this the default globally so I do not have to add it to every property ?

如何将其设为全局默认值,以便不必将其添加到每个属性中?

Using XML configuration.

使用 XML 配置。

回答by Manu

If you are using Java-based configuration, you can create your configuration class extending WebMvcConfigurerAdapter and do the following:

如果您使用基于 Java 的配置,您可以创建扩展 WebMvcConfigurerAdapter 的配置类并执行以下操作:

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    final MappingHymanson2HttpMessageConverter converter = new MappingHymanson2HttpMessageConverter();
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    converter.setObjectMapper(objectMapper);
    converters.add(converter);
    super.configureMessageConverters(converters);
}

In here, you can configure the ObjectMapper as you like and set it as a converter.

在这里,您可以根据需要配置 ObjectMapper 并将其设置为转换器。

回答by Jaiwo99

Well you can install modules for using things like java datetime or jodatime. checkout this:

好吧,您可以安装模块以使用诸如 java datetime 或 jodatime 之类的东西。结帐这个:

    @Bean
    Hymanson2ObjectMapperBuilder Hymanson2ObjectMapperBuilder() {
        JavaTimeModule module = new JavaTimeModule();
        module.addSerializer(OffsetDateTime.class, JSR310DateTimeSerializer.INSTANCE);
        module.addSerializer(ZonedDateTime.class, JSR310DateTimeSerializer.INSTANCE);
        module.addSerializer(LocalDateTime.class, JSR310DateTimeSerializer.INSTANCE);
        module.addSerializer(Instant.class, JSR310DateTimeSerializer.INSTANCE);
        module.addDeserializer(LocalDate.class, JSR310LocalDateDeserializer.INSTANCE);
        return new Hymanson2ObjectMapperBuilder()
                .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                .findModulesViaServiceLoader(true)
                .modulesToInstall(module);
    }

回答by Laksitha Ranasingha

You can use configure your serializers (providing fully qualified class name) in spring Hymanson2ObjectMapperFactoryBeanthen bind it with MappingHymanson2HttpMessageConverter. Here is an example XMLconfiguration snippet:

您可以在 spring 中使用配置序列化程序(提供完全限定的类名),Hymanson2ObjectMapperFactoryBean然后将其与 MappingHymanson2HttpMessageConverter. 这是一个示例XML配置片段:

<bean class="org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter">
   <property name="objectMapper">
     <bean class="org.springframework.web.context.support.Hymanson2ObjectMapperFactoryBean"
       p:failOnEmptyBeans="false"
       p:indentOutput="true">
       <property name="serializers">
         <array>
           <bean class="LocalDateSerializer" />
         </array>
       </property>
     </bean>
   </property>
 </bean>

The linkto the documentation

链接的文档

回答by Jan Kopecky

With Spring Boot you can achieve this by registering new Module.

使用 Spring Boot,您可以通过注册 new 来实现这一点Module

@Configuration
public class AppConfig {

    @Bean
    public Module module() {
        SimpleModule module = new SimpleModule("Module", new Version(1, 0, 0, null, null, null));
        module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
        module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());

        return module;
    }
}

As stated in documentation here

正如文件指出这里

Hymanson 1.7 added ability to register serializers and deserializes via Module interface. This is the recommended way to add custom serializers -- all serializers are considered "generic", in that they are used for subtypes unless more specific binding is found.

Hymanson 1.7 添加了通过 Module 接口注册序列化器和反序列化的能力。这是添加自定义序列化程序的推荐方法——所有序列化程序都被认为是“通用的”,因为它们用于子类型,除非找到更具体的绑定。

and here:

这里

Any beans of type com.fasterxml.Hymanson.databind.Moduleare automatically registered with the auto-configured Hymanson2ObjectMapperBuilderand are applied to any ObjectMapperinstances that it creates. This provides a global mechanism for contributing custom modules when you add new features to your application.

任何类型的 beans 都会com.fasterxml.Hymanson.databind.Module自动注册到 auto-configured Hymanson2ObjectMapperBuilder并应用于ObjectMapper它创建的任何实例。当您向应用程序添加新功能时,这提供了用于贡献自定义模块的全局机制。