아래 강의의 요약 자료입니다.
inflearn:
youtube:
git: https://github.com/ChunGeun-Yu/springboot-usingwell
ObjectMapper
LocalDateTime 직렬화 문제있는 설정:
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper();
}
LocalDateTime 을 설정한 bean을 등록하는 예제:
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
JavaTimeModule javaTimeModule = new JavaTimeModule();
LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyyMMdd hhmmss.SSS"));
javaTimeModule.addSerializer(LocalDateTime.class, localDateTimeSerializer);
objectMapper.registerModule(javaTimeModule);
return objectMapper;
}
Jackson2ObjectMapperBuilderCustomizer 을 통해 LocalDateTime 직렬화부분만 설정하는 예제:
@Bean
public Jackson2ObjectMapperBuilderCustomizer objectMapperBuilderCustomizer() {
return new Jackson2ObjectMapperBuilderCustomizer() {
@Override
public void customize(Jackson2ObjectMapperBuilder builder) {
LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyyMMdd hhmmss.SSS"));
builder.serializerByType(LocalDateTime.class, localDateTimeSerializer);
}
};
}
Jackson2ObjectMapperBuilder 에 LocalDateTime 직렬화부분만 설정하는 예제:
@Bean
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyyMMdd hhmmss.SSS"));
return builder
.serializerByType(LocalDateTime.class, localDateTimeSerializer)
.build();
}
RestTemplate
traceId 전파안되는 RestTemplate 설정:
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
return restTemplate;
}
RestTemplateBuilder 를 이용해서 bean 등록하는 설정:
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
RestClient
traceId 전파 안되는 설정:
@Bean
public RestClient restClient() {
return RestClient.create();
}
RestClient.Builder 을 이용해서 bean 등록하는 예제:
@Bean
public RestClient restClient(RestClient.Builder builder) {
return builder.build();
}
ThreadPoolTaskExecutor
threadlocal 복사하는 TaskDecorator:
public class ThreadLocalCopyTaskDecorator implements TaskDecorator {
@Override
public Runnable decorate(Runnable runnable) {
Map<String, String> map = MDC.getCopyOfContextMap();
return new Runnable() {
@Override
public void run() {
if (map != null)
MDC.setContextMap(map);
try {
runnable.run();
} finally {
MDC.clear();
}
}
};
}
}
TaskDecorator 직접 지정하는 예제:
@Bean
public ThreadPoolTaskExecutor threadPoolTaskExecutor(ThreadPoolTaskExecutorBuilder builder) {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setThreadNamePrefix("myThread-");
executor.setTaskDecorator(new ThreadLocalCopyTaskDecorator());
executor.initialize();
return executor;
}
TaskDecorator 만 직접 등록하고, 나머지는 springboot 에 위임하는 예제:
@Bean
public TaskDecorator myTaskDecorator() {
return new ThreadLocalCopyTaskDecorator();
}
yml 을 통해 ThreadPoolTaskExecutor 설정하는 예제:
spring:
task:
execution:
thread-name-prefix: myTask-