Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 带spring启动的json序列化程序_Spring Boot_Jackson_Java Custom Serialization - Fatal编程技术网

Spring boot 带spring启动的json序列化程序

Spring boot 带spring启动的json序列化程序,spring-boot,jackson,java-custom-serialization,Spring Boot,Jackson,Java Custom Serialization,我有一个大小数序列化程序 public class BigDecimalSerializer extends JsonSerializer<BigDecimal> { @Override public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeString(value.setScale(6, B

我有一个大小数序列化程序

public class BigDecimalSerializer extends JsonSerializer<BigDecimal> {

@Override
public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers)
  throws IOException {
gen.writeString(value.setScale(6, BigDecimal.ROUND_HALF_UP).toString());
}
}

是否有任何方法可以代替在每个成员变量中进行注释,而立即告诉spring boot应用于所有项目?

尝试通过添加自定义模块来配置
ObjectMapper
。如果您使用的是
spring data rest
,则如下所示:

@Configuration
public static class ObjectMapperConfigurer extends RepositoryRestConfigurerAdapter {
    @Override
    public void configureJacksonObjectMapper(final ObjectMapper objectMapper) {
        SimpleModule myModule = new SimpleModule();
        myModule.addSerializer(BigDecimal.class, BigDecimalSerializer.class);           
        objectMapper.registerModule(myModule));
    }
}

否则,只需提供您自己的
ObjectMapper
bean,并在创建时对其进行配置。

repositoryrestconfiguureradapter
@Configuration
public static class ObjectMapperConfigurer extends RepositoryRestConfigurerAdapter {
    @Override
    public void configureJacksonObjectMapper(final ObjectMapper objectMapper) {
        SimpleModule myModule = new SimpleModule();
        myModule.addSerializer(BigDecimal.class, BigDecimalSerializer.class);           
        objectMapper.registerModule(myModule));
    }
}