Rev 23701 | Rev 23703 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.web.config;import java.time.format.DateTimeFormatter;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.http.converter.HttpMessageConverter;import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;import org.springframework.web.servlet.config.annotation.CorsRegistry;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.SerializationFeature;import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;import com.spice.profitmandi.common.model.ProfitMandiConstants;import com.spice.profitmandi.common.web.interceptor.SimpleCORSInterceptor;import com.spice.profitmandi.web.interceptor.AuthenticationInterceptor;import springfox.documentation.builders.PathSelectors;import springfox.documentation.builders.RequestHandlerSelectors;import springfox.documentation.service.ApiInfo;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration@EnableWebMvc@EnableSwagger2@ComponentScan("com.spice.profitmandi.*")public class WebMVCConfig extends WebMvcConfigurerAdapter{private static final String RESOURCES_PATTERN="/resources/**";private static final String RESOURCES_LOCATION="/resources/";public static final DateTimeFormatter FORMATTER =DateTimeFormatter.ofPattern("dd::MM::yyyy");@AutowiredSimpleCORSInterceptor simpleCORSInterceptor;@AutowiredAuthenticationInterceptor authenticationInterceptor;@Beanpublic Docket api() {return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build().apiInfo(apiInfo());}@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");registry.addResourceHandler(RESOURCES_PATTERN).addResourceLocations(RESOURCES_LOCATION);}@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(simpleCORSInterceptor).addPathPatterns("/**").excludePathPatterns("/swagger-ui.html", ProfitMandiConstants.URL_PAYU_PAY_RESPONSE, ProfitMandiConstants.URL_PAYU_PAY_CANCELLED);registry.addInterceptor(authenticationInterceptor).addPathPatterns("/**").excludePathPatterns(ProfitMandiConstants.URL_ADMIN_TOKEN).excludePathPatterns("/**/swagger*/**").excludePathPatterns("/v2/**").excludePathPatterns("/document").excludePathPatterns(ProfitMandiConstants.URL_USER_GOOGLE_LOGIN, ProfitMandiConstants.URL_USER_GOOGLE_LOGIN + "/", ProfitMandiConstants.URL_VERIFY_OTP);//registry to check api access on basis of UserInfo}@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**").allowedOrigins("*").allowedMethods("*").allowedHeaders("*").allowCredentials(false);}private ApiInfo apiInfo() {@SuppressWarnings("deprecation")ApiInfo apiInfo = new ApiInfo("ProfitMandi API","Api's for profitmandi app","API TOS","Terms of service","New Spice Solutions Pvt. Ltd.","License of API","API license URL");return apiInfo;}@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {MappingJackson2HttpMessageConverter jacksonMessageConverter = new MappingJackson2HttpMessageConverter();ObjectMapper objectMapper = jacksonMessageConverter.getObjectMapper();objectMapper.registerModule(new JavaTimeModule());objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);converters.add(jacksonMessageConverter);}/* private ObjectMapper objectMapper() {ObjectMapper objectMapper = new ObjectMapper();objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);JavaTimeModule javaTimeModule = new JavaTimeModule();javaTimeModule.addSerializer(java.sql.Date.class, new DateSerializer());//javaTimeModule.addDeserializer(Timestamp.class, new LocalDateDeserializer());objectMapper.registerModule(javaTimeModule);return objectMapper;}public class LocalDateSerializer extends JsonSerializer<Timestamp> {@Overridepublic void serialize(Timestamp value, JsonGenerator gen, SerializerProvider serializers) throws IOException {ZonedDateTime z = value.toInstant().atZone(ZoneId.systemDefault());String str = FORMATTER.format(z);gen.writeString(str);}}*//* public class LocalDateDeserializer extends JsonDeserializer<Timestamp> {@Overridepublic Timestamp deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {LocalDateTime dt = LocalDateTime.parse(p.getText(), FORMATTER);// the date/time is in the default timezonereturn Timestamp.from(dt.atZone(ZoneId.systemDefault()).toInstant());}}*/}