Port to java 17
[ccsdk/apps.git] / ms / sliboot / src / main / templates / libraries / spring-mvc / swaggerUiConfiguration.mustache
1 package {{configPackage}};
2
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import com.fasterxml.jackson.databind.SerializationFeature;
5 {{#threetenbp}}
6 import com.fasterxml.jackson.datatype.threetenbp.ThreeTenModule;
7 {{/threetenbp}}
8 import org.springframework.context.annotation.ComponentScan;
9 import org.springframework.context.annotation.Configuration;
10 import org.springframework.context.annotation.PropertySource;
11 import org.springframework.context.annotation.Import;
12 import org.springframework.context.annotation.Bean;
13 import org.springframework.format.FormatterRegistry;
14 import org.springframework.http.converter.HttpMessageConverter;
15 import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
16 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
17 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
18 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
19 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
20 {{#threetenbp}}
21 import org.threeten.bp.Instant;
22 import org.threeten.bp.OffsetDateTime;
23 import org.threeten.bp.ZonedDateTime;
24 {{/threetenbp}}
25 import springfox.documentation.swagger2.annotations.EnableSwagger2;
26
27 import java.util.List;
28
29 {{>generatedAnnotation}}
30 @Configuration
31 @ComponentScan(basePackages = "{{apiPackage}}")
32 @EnableWebMvc
33 @EnableSwagger2 //Loads the spring beans required by the framework
34 @PropertySource("classpath:swagger.properties")
35 @Import(SwaggerDocumentationConfig.class)
36 public class SwaggerUiConfiguration implements WebMvcConfigurer {
37   private static final String[] SERVLET_RESOURCE_LOCATIONS = { "/" };
38
39   private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
40       "classpath:/META-INF/resources/", "classpath:/resources/",
41       "classpath:/static/", "classpath:/public/" };
42
43   private static final String[] RESOURCE_LOCATIONS;
44   static {
45     RESOURCE_LOCATIONS = new String[CLASSPATH_RESOURCE_LOCATIONS.length
46         + SERVLET_RESOURCE_LOCATIONS.length];
47     System.arraycopy(SERVLET_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, 0,
48         SERVLET_RESOURCE_LOCATIONS.length);
49     System.arraycopy(CLASSPATH_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS,
50         SERVLET_RESOURCE_LOCATIONS.length, CLASSPATH_RESOURCE_LOCATIONS.length);
51   }
52
53   private static final String[] STATIC_INDEX_HTML_RESOURCES;
54   static {
55     STATIC_INDEX_HTML_RESOURCES = new String[RESOURCE_LOCATIONS.length];
56     for (int i = 0; i < STATIC_INDEX_HTML_RESOURCES.length; i++) {
57       STATIC_INDEX_HTML_RESOURCES[i] = RESOURCE_LOCATIONS[i] + "index.html";
58     }
59   }
60
61   @Override
62   public void addResourceHandlers(ResourceHandlerRegistry registry) {
63     if (!registry.hasMappingForPattern("/webjars/**")) {
64       registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
65     }
66     if (!registry.hasMappingForPattern("/**")) {
67       registry.addResourceHandler("/**").addResourceLocations(RESOURCE_LOCATIONS);
68     }
69   }
70
71   @Bean
72   public Jackson2ObjectMapperBuilder builder() {
73     Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
74         .indentOutput(true)
75         .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
76         .dateFormat(new RFC3339DateFormat());
77     return builder;
78   }
79
80   @Override
81   public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
82     converters.add(new MappingJackson2HttpMessageConverter(objectMapper()));
83     super.configureMessageConverters(converters);
84   }
85
86   @Override
87   public void addFormatters(FormatterRegistry registry) {
88     registry.addConverter(new LocalDateConverter("{{#datePattern}}{{datePattern}}{{/datePattern}}{{^datePattern}}yyyy-MM-dd{{/datePattern}}"));
89     registry.addConverter(new LocalDateTimeConverter("{{#dateTimePattern}}{{dateTimePattern}}{{/dateTimePattern}}{{^dateTimePattern}}yyyy-MM-dd'T'HH:mm:ss.SSS{{/dateTimePattern}}"));
90   }
91
92   @Bean
93   public ObjectMapper objectMapper(){
94     return builder().build();
95   }
96 }