From d233943fc29508dc37714852b11b96b0b46b55d9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 24 Nov 2016 21:38:52 +0300 Subject: juick-api: now on spring-webmvc --- .../api/configuration/ApiAppConfiguration.java | 29 +++++++++++++++ .../juick/api/configuration/ApiInitializer.java | 39 ++++++++++++++++++++ .../api/configuration/ApiMvcConfiguration.java | 43 ++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java create mode 100644 juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java create mode 100644 juick-api/src/main/java/com/juick/api/configuration/ApiMvcConfiguration.java (limited to 'juick-api/src/main/java/com/juick/api/configuration') diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java b/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java new file mode 100644 index 00000000..8f1e269b --- /dev/null +++ b/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java @@ -0,0 +1,29 @@ +package com.juick.api.configuration; + +import com.juick.api.ApiServer; +import com.juick.api.TGBot; +import com.juick.configuration.DataConfiguration; +import org.springframework.context.annotation.*; +import org.springframework.core.env.Environment; + +import javax.inject.Inject; + +/** + * Created by aalexeev on 11/12/16. + */ +@Configuration +@PropertySource("classpath:juick.conf") +@Import(DataConfiguration.class) +public class ApiAppConfiguration { + @Inject + private Environment env; + + @Bean + ApiServer apiServer() { + return new ApiServer(env); + } + @Bean + TGBot tgBot() { + return new TGBot(env.getProperty("telegram_token", "")); + } +} diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java b/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java new file mode 100644 index 00000000..f5ba4ff1 --- /dev/null +++ b/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java @@ -0,0 +1,39 @@ +package com.juick.api.configuration; + +import org.springframework.web.filter.CharacterEncodingFilter; +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +import javax.servlet.Filter; + +/** + * Created by vt on 09/02/16. + */ +public class ApiInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { + + @Override + protected Class[] getRootConfigClasses() { + return new Class[]{ApiAppConfiguration.class}; + } + + @Override + protected Class[] getServletConfigClasses() { + return new Class[]{ApiMvcConfiguration.class}; + } + + @Override + protected String[] getServletMappings() { + return new String[]{"/"}; + } + + @Override + protected Filter[] getServletFilters() { + CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); + characterEncodingFilter.setEncoding("UTF-8"); + return new Filter[]{characterEncodingFilter}; + } + + @Override + protected String getServletName() { + return "API dispatcher servlet"; + } +} diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiMvcConfiguration.java b/juick-api/src/main/java/com/juick/api/configuration/ApiMvcConfiguration.java new file mode 100644 index 00000000..7183ab1f --- /dev/null +++ b/juick-api/src/main/java/com/juick/api/configuration/ApiMvcConfiguration.java @@ -0,0 +1,43 @@ +package com.juick.api.configuration; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.juick.api.ApiServer; +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.Jackson2ObjectMapperBuilder; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; + +import java.util.List; + +/** + * Created by vitalyster on 28.06.2016. + */ +@Configuration +@ComponentScan(basePackages = {"com.juick.api"}) +public class ApiMvcConfiguration extends WebMvcConfigurationSupport { + + @Override + public RequestMappingHandlerMapping requestMappingHandlerMapping() { + RequestMappingHandlerMapping mapping = super.requestMappingHandlerMapping(); + mapping.setUseSuffixPatternMatch(false); + return mapping; + } + + @Override + protected void configureMessageConverters(List> converters) { + Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder() + .serializationInclusion(JsonInclude.Include.NON_DEFAULT) + .serializationInclusion(JsonInclude.Include.NON_NULL) + .serializationInclusion(JsonInclude.Include.NON_ABSENT) + .serializationInclusion(JsonInclude.Include.NON_EMPTY); + MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(builder.build()); + converter.getObjectMapper().registerModule(new Jdk8Module()); + converters.add(converter); + super.configureMessageConverters(converters); + } +} -- cgit v1.2.3