org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer Java Examples

The following examples show how to use org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JacksonConfig.java    From dts-shop with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public Jackson2ObjectMapperBuilderCustomizer customJackson() {
	return new Jackson2ObjectMapperBuilderCustomizer() {
		@Override
		public void customize(Jackson2ObjectMapperBuilder builder) {
			builder.serializerByType(LocalDateTime.class,
					new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
			builder.serializerByType(LocalDate.class,
					new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
			builder.serializerByType(LocalTime.class,
					new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));

			builder.deserializerByType(LocalDateTime.class,
					new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
			builder.deserializerByType(LocalDate.class,
					new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
			builder.deserializerByType(LocalTime.class,
					new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
			builder.serializationInclusion(JsonInclude.Include.NON_NULL);
			builder.failOnUnknownProperties(false);
			builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
		}
	};
}
 
Example #2
Source File: JacksonConfig.java    From BigDataPlatform with GNU General Public License v3.0 6 votes vote down vote up
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public Jackson2ObjectMapperBuilderCustomizer customJackson() {
    return new Jackson2ObjectMapperBuilderCustomizer() {
        @Override
        public void customize(Jackson2ObjectMapperBuilder builder) {
            builder.serializerByType(LocalDateTime.class,
                    new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            builder.serializerByType(LocalDate.class,
                    new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            builder.serializerByType(LocalTime.class,
                    new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
            builder.deserializerByType(LocalDateTime.class,
                    new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            builder.deserializerByType(LocalDate.class,
                    new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            builder.deserializerByType(LocalTime.class,
                    new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
            builder.serializationInclusion(JsonInclude.Include.NON_NULL);
            builder.failOnUnknownProperties(false);
            builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        }
    };
}
 
Example #3
Source File: JacksonConfig.java    From syhthems-platform with MIT License 6 votes vote down vote up
@Bean
public Jackson2ObjectMapperBuilderCustomizer customJackson() {
    return builder -> builder
            // 在序列化时将日期转化为时间戳
            .featuresToEnable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .featuresToEnable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
            // 在序列化枚举对象时使用toString方法
            .featuresToEnable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
            // 在反序列化枚举对象时使用toString方法
            .featuresToEnable(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
            .featuresToEnable(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS)
            // 日期和时间格式:"yyyy-MM-dd HH:mm:ss"
            // .simpleDateFormat(BaseConstants.DATETIME_FORMAT)
            .createXmlMapper(false)
            .timeZone("GMT+8:00")
            ;
}
 
Example #4
Source File: JacksonConfig.java    From mall with MIT License 6 votes vote down vote up
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public Jackson2ObjectMapperBuilderCustomizer customJackson() {
    return new Jackson2ObjectMapperBuilderCustomizer() {
        @Override
        public void customize(Jackson2ObjectMapperBuilder builder) {
            builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            builder.serializerByType(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            builder.serializerByType(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));

            builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            builder.deserializerByType(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            builder.deserializerByType(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
            builder.serializationInclusion(JsonInclude.Include.NON_NULL);
            builder.failOnUnknownProperties(false);
            builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        }
    };
}
 
Example #5
Source File: JacksonConfig.java    From litemall with MIT License 6 votes vote down vote up
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public Jackson2ObjectMapperBuilderCustomizer customJackson() {
    return new Jackson2ObjectMapperBuilderCustomizer() {
        @Override
        public void customize(Jackson2ObjectMapperBuilder builder) {
            builder.serializerByType(LocalDateTime.class,
                    new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            builder.serializerByType(LocalDate.class,
                    new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            builder.serializerByType(LocalTime.class,
                    new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
            builder.deserializerByType(LocalDateTime.class,
                    new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            builder.deserializerByType(LocalDate.class,
                    new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            builder.deserializerByType(LocalTime.class,
                    new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
            builder.serializationInclusion(JsonInclude.Include.NON_NULL);
            builder.failOnUnknownProperties(false);
            builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        }
    };
}
 
Example #6
Source File: ProtobufConfiguration.java    From jigsaw-payment with Apache License 2.0 6 votes vote down vote up
@Bean
public Jackson2ObjectMapperBuilderCustomizer configProtobufSerializer() {
	return new Jackson2ObjectMapperBuilderCustomizer() {

		@Override
		public void customize(
				Jackson2ObjectMapperBuilder builder) {
			builder.serializerByType(Message.class, new JsonSerializer<Message>(){

				@Override
				public void serialize(Message message, JsonGenerator generator,
						SerializerProvider provider) throws IOException {
					if(message == null)
						return;
					JsonJacksonFormat format = new JsonJacksonFormat();
					format.print(message, generator);
				}});
			
		}
	};
}
 
Example #7
Source File: ApisAutoConfiguration.java    From genie with Apache License 2.0 5 votes vote down vote up
/**
 * Customizer for {@link com.fasterxml.jackson.databind.ObjectMapper} used by controllers.
 *
 * @return an {@link Jackson2ObjectMapperBuilderCustomizer}
 */
@Bean
public Jackson2ObjectMapperBuilderCustomizer apisObjectMapperCustomizer() {
    return builder -> {
        // Register the same PropertyFilters that are registered in the static GenieObjectMapper
        builder.filters(GenieObjectMapper.FILTER_PROVIDER);
    };
}
 
Example #8
Source File: ContactAppConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
@ConditionalOnProperty(value = "spring.jackson.date-format", matchIfMissing = true, havingValue = "none")
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
    return new Jackson2ObjectMapperBuilderCustomizer() {
        @Override
        public void customize(Jackson2ObjectMapperBuilder builder) {
            builder.simpleDateFormat(dateTimeFormat);
            builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(dateFormat)));
            builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)));
        }
    };
}
 
Example #9
Source File: ContactAppConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
@ConditionalOnProperty(value = "spring.jackson.date-format", matchIfMissing = true, havingValue = "none")
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
    return new Jackson2ObjectMapperBuilderCustomizer() {
        @Override
        public void customize(Jackson2ObjectMapperBuilder builder) {
            builder.simpleDateFormat(dateTimeFormat);
            builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(dateFormat)));
            builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)));
        }
    };
}
 
Example #10
Source File: MainApp.java    From botanic-ng with Apache License 2.0 5 votes vote down vote up
@Bean
public Jackson2ObjectMapperBuilderCustomizer addCustomBigDecimalDeserialization() {
	return new Jackson2ObjectMapperBuilderCustomizer() {

		@Override
		public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
			jacksonObjectMapperBuilder.dateFormat(new ISO8601DateFormatWithMilliSeconds());
		}

	};
}
 
Example #11
Source File: TestDependencies.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Bean
public Jackson2ObjectMapperBuilderCustomizer dataflowObjectMapperBuilderCustomizer() {
	return (builder) -> {
		builder.dateFormat(new ISO8601DateFormatWithMilliSeconds());
		builder.mixIn(StepExecution.class, StepExecutionJacksonMixIn.class);
		builder.mixIn(ExecutionContext.class, ExecutionContextJacksonMixIn.class);
		builder.modules(new JavaTimeModule());
	};
}
 
Example #12
Source File: WebConfiguration.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Bean
public Jackson2ObjectMapperBuilderCustomizer dataflowObjectMapperBuilderCustomizer() {
	return (builder) -> {
		builder.dateFormat(new ISO8601DateFormatWithMilliSeconds(TimeZone.getDefault(), Locale.getDefault(), true));
		// apply SCDF Batch Mixins to
		// ignore the JobExecution in StepExecution to prevent infinite loop.
		// https://github.com/spring-projects/spring-hateoas/issues/333
		builder.mixIn(StepExecution.class, StepExecutionJacksonMixIn.class);
		builder.mixIn(ExecutionContext.class, ExecutionContextJacksonMixIn.class);
		builder.modules(new JavaTimeModule(), new Jdk8Module());
	};
}
 
Example #13
Source File: JacksonConfiguration.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Bean
public Jackson2ObjectMapperBuilderCustomizer objectMapperBuilderCustomizer() {
    return new Jackson2ObjectMapperBuilderCustomizer() {

        @Override
        public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
            jacksonObjectMapperBuilder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        }
    };
}
 
Example #14
Source File: DateFormatConfigure.java    From cms with Apache License 2.0 5 votes vote down vote up
/**
 * 自定义Bean
 *
 * @return
 */
@Bean
@Primary
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
    return builder -> builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATETIME_FORMAT)))
            .serializerByType(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DATE_FORMAT)))
            .serializerByType(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(TIME_FORMAT)))
            .deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DATETIME_FORMAT)))
            .deserializerByType(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DATE_FORMAT)))
            .deserializerByType(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(TIME_FORMAT)));
}
 
Example #15
Source File: PluginConfig.java    From kafka-webview with MIT License 5 votes vote down vote up
/**
 * Customize the jackson object map builder.
 * @return Jackson2ObjectMapperBuilderCustomizer instance.
 */
@Bean
public Jackson2ObjectMapperBuilderCustomizer registerJacksonProtobufModule() {
    return jacksonObjectMapperBuilder -> {
        // Register custom protocol buffer serializer.
        jacksonObjectMapperBuilder.modulesToInstall(new ProtobufModule());
    };
}
 
Example #16
Source File: TimerAutoConfiguration.java    From jigsaw-payment with Apache License 2.0 5 votes vote down vote up
@Bean
public Jackson2ObjectMapperBuilderCustomizer configBuilder() {
	return new Jackson2ObjectMapperBuilderCustomizer() {

		@Override
		public void customize(
				Jackson2ObjectMapperBuilder builder) {
			builder.findModulesViaServiceLoader(true)
					.dateFormat(new SimpleDateFormat("yyyy-MM-dd"));
			
		}

	};
}
 
Example #17
Source File: JacksonConfig.java    From albedo with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
public Jackson2ObjectMapperBuilderCustomizer customizer() {
	return builder -> {
		builder.locale(Locale.CHINA);
		builder.timeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
		builder.simpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
		builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
		builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
		builder.deserializers(new LocalDateDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
		builder.deserializers(new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
		builder.modules(new JavaTimeModule());
	};
}
 
Example #18
Source File: JacksonConfig.java    From x7 with Apache License 2.0 5 votes vote down vote up
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
    Jackson2ObjectMapperBuilderCustomizer cunstomizer = new Jackson2ObjectMapperBuilderCustomizer() {

        @Override
        public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {

            jacksonObjectMapperBuilder.serializerByType(Date.class, new DateToLongSerializer());
        }
    };

    return cunstomizer;
}
 
Example #19
Source File: JacksonConfig.java    From black-shop with Apache License 2.0 5 votes vote down vote up
@Bean
public Jackson2ObjectMapperBuilderCustomizer customizer() {
	return builder -> {
		builder.locale(Locale.CHINA);
		builder.timeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
		builder.simpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
		builder.modules(new BsJavaTimeModule());
	};
}
 
Example #20
Source File: DateTimeFormatConfig.java    From yue-library with Apache License 2.0 5 votes vote down vote up
/**
* 关于日期时间反序列化,只有在使用 {@link RequestBody} 时有效
* 
* @return 自定义序列化器
*/
  @Bean
  public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
      return builder -> builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DateUtils.DATE_TIME_FORMATTER))
              .serializerByType(LocalDate.class, new LocalDateSerializer(DateUtils.DATE_FORMATTER))
              .serializerByType(LocalTime.class, new LocalTimeSerializer(DateUtils.TIME_FORMATTER))
              .deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(DateUtils.DATE_TIME_FORMATTER))
              .deserializerByType(LocalDate.class, new LocalDateDeserializer(DateUtils.DATE_FORMATTER))
              .deserializerByType(LocalTime.class, new LocalTimeDeserializer(DateUtils.TIME_FORMATTER));
  }
 
Example #21
Source File: JacksonConfiguration.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Bean()
public Jackson2ObjectMapperBuilderCustomizer objectMapperBuilderCustomizer() {
    return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
}
 
Example #22
Source File: LocalDateTimeSerializerConfiguration.java    From oauth2-resource with MIT License 4 votes vote down vote up
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
    return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer());
}
 
Example #23
Source File: JacksonConfiguration.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Bean()
public Jackson2ObjectMapperBuilderCustomizer objectMapperBuilderCustomizer() {
    return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
}
 
Example #24
Source File: SpringComponentsConfiguration.java    From api-layer with Eclipse Public License 2.0 4 votes vote down vote up
@Bean
public Jackson2ObjectMapperBuilderCustomizer failOnUnknownProperties() {
    return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder
        .featuresToEnable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
 
Example #25
Source File: LocalDateTimeSerializerConfiguration.java    From oauth2-server with MIT License 4 votes vote down vote up
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
    return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer());
}
 
Example #26
Source File: WebConfig.java    From youran with Apache License 2.0 4 votes vote down vote up
/**
 * 防止通过body传入XSS脚本
 *
 * @return
 */
@Bean
public Jackson2ObjectMapperBuilderCustomizer jacksonXSSCustomizer() {
    return jacksonObjectMapperBuilder ->
        jacksonObjectMapperBuilder.deserializerByType(String.class, new JacksonXSSDeserializer());
}
 
Example #27
Source File: ApplicationConfig.java    From RuoYi-Vue with MIT License 4 votes vote down vote up
/**
 * 时区配置
 */
@Bean
public Jackson2ObjectMapperBuilderCustomizer jacksonObjectMapperCustomization()
{
    return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder.timeZone(TimeZone.getDefault());
}