1 package org.imageconverter.config;
2
3 import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;
4 import static com.fasterxml.jackson.databind.MapperFeature.ALLOW_COERCION_OF_SCALARS;
5
6 import java.time.format.DateTimeFormatter;
7
8 import org.springframework.boot.context.properties.EnableConfigurationProperties;
9 import org.springframework.context.MessageSource;
10 import org.springframework.context.annotation.Bean;
11 import org.springframework.context.annotation.Configuration;
12 import org.springframework.context.annotation.EnableAspectJAutoProxy;
13 import org.springframework.context.annotation.Primary;
14 import org.springframework.context.support.ResourceBundleMessageSource;
15 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
16 import org.springframework.transaction.annotation.EnableTransactionManagement;
17 import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
18 import org.springframework.web.multipart.commons.CommonsMultipartResolver;
19 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
20 import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
21 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
22 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
23 import org.springframework.web.util.UrlPathHelper;
24
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.fasterxml.jackson.databind.json.JsonMapper;
27 import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
28 import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
29 import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
30 import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
31
32 import brave.sampler.Sampler;
33
34
35
36
37
38
39 @Configuration
40 @EnableWebSecurity
41 @EnableWebMvc
42 @EnableAspectJAutoProxy
43 @EnableConfigurationProperties
44 @EnableTransactionManagement
45 public class WebConfig implements WebMvcConfigurer {
46
47
48
49
50 @Override
51 public void configurePathMatch(final PathMatchConfigurer configurer) {
52
53 final var urlPathHelper = new UrlPathHelper();
54 urlPathHelper.setRemoveSemicolonContent(false);
55 configurer.setUrlPathHelper(urlPathHelper);
56 }
57
58
59
60
61 @Override
62 public void addResourceHandlers(final ResourceHandlerRegistry registry) {
63 registry.addResourceHandler("/swagger-ui/**")
64 .addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.1.3/");
65 }
66
67 @Bean
68 Sampler defaultSampler() {
69 return Sampler.ALWAYS_SAMPLE;
70 }
71
72
73
74
75
76
77 @Bean
78 @Primary
79 ObjectMapper objectMapper() {
80 final var module = new JavaTimeModule();
81 module.addSerializer(new LocalDateSerializer(DateTimeFormatter.ISO_DATE));
82 module.addSerializer(new LocalDateTimeSerializer(DateTimeFormatter.ISO_DATE_TIME));
83 module.addSerializer(new LocalTimeSerializer(DateTimeFormatter.ISO_TIME));
84
85 return JsonMapper.builder()
86 .configure(ALLOW_COERCION_OF_SCALARS, false)
87 .serializationInclusion(NON_NULL)
88 .addModule(module)
89 .build();
90 }
91
92
93
94
95
96
97
98
99
100 @Bean
101 MessageSource messageSource() {
102 final var messageSource = new ResourceBundleMessageSource();
103 messageSource.setBasename("messages");
104 messageSource.setDefaultEncoding("UTF-8");
105 messageSource.setUseCodeAsDefaultMessage(true);
106 return messageSource;
107 }
108
109
110
111
112
113 @Bean
114 @Primary
115 @Override
116 public LocalValidatorFactoryBean getValidator() {
117 final var bean = new LocalValidatorFactoryBean();
118 bean.setValidationMessageSource(messageSource());
119 return bean;
120 }
121
122
123
124
125
126
127
128 @Bean
129 CommonsMultipartResolver multipartResolver() {
130 final var resolver = new CommonsMultipartResolver();
131 resolver.setDefaultEncoding("utf-8");
132
133
134 return resolver;
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 }