package me.ocheng.common.config;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import me.ocheng.common.converter.AbstractHttpMessageConverter;
import me.ocheng.common.core.ApplicationContext;
import me.ocheng.common.interceptor.AbstractInterceptor;
import me.ocheng.common.resolvers.AbstractMethodArgumentResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.*;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.*;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
*/
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private HttpMessageConverters messageConverters;
@Override
public void addInterceptors(InterceptorRegistry registry) {
Map interceptors = ApplicationContext.getInstance().getBeansOfType(AbstractInterceptor.class);
Iterator iterable = interceptors.values().iterator();
while (iterable.hasNext()) {
registry.addInterceptor(iterable.next());
}
}
@Override
public void addArgumentResolvers(List argumentResolvers) {
Map interceptors = ApplicationContext.getInstance().getBeansOfType(AbstractMethodArgumentResolver.class);
Iterator iterable = interceptors.values().iterator();
while (iterable.hasNext()) {
argumentResolvers.add(iterable.next());
}
}
@Override
public void configureMessageConverters(List> converters) {
// converters.addAll(messageConverters.getConverters());
converters.add(new ByteArrayHttpMessageConverter());
converters.add(new StringHttpMessageConverter());
converters.add(new ResourceHttpMessageConverter());
converters.add(new ResourceRegionHttpMessageConverter());
converters.add(new SourceHttpMessageConverter<>());
converters.add(new AllEncompassingFormHttpMessageConverter());
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
xmlMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
converters.add(new MappingJackson2XmlHttpMessageConverter(xmlMapper));
Map interceptors = ApplicationContext.getInstance().getBeansOfType(AbstractHttpMessageConverter.class);
Iterator iterable = interceptors.values().iterator();
while (iterable.hasNext()) {
converters.add((HttpMessageConverter>) iterable.next());
}
}
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.defaultContentType(MediaType.APPLICATION_JSON);
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*")
.exposedHeaders("X-Security-Token", "X-Security-Token-Renewal", "Proxy-Connection", "Accept", "Cache-Control", "Content-Type", "Content-Range", "DNT", "If-Modified-Since", "Keep-Alive", "Origin", "User-Agent", "X-Requested-With")
.maxAge(3600);
}
}
package me.ocheng.common.converter;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import java.io.IOException;
import java.util.List;
/**
* @date 2018-06-22
*/
public abstract class AbstractHttpMessageConverter implements HttpMessageConverter {
@Override
public abstract boolean canRead(Class> clazz, MediaType mediaType);
@Override
public abstract boolean canWrite(Class> clazz, MediaType mediaType);
@Override
public abstract List getSupportedMediaTypes();
@Override
public abstract T read(Class extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException;
@Override
public abstract void write(T t, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException;
}
package me.ocheng.common.core;
import org.springframework.core.env.Environment;
import java.util.Map;
/**
* @author Eric
* @date 2017/2/22
*/
public class ApplicationContext {
private final static ApplicationContext INSTANCE = new ApplicationContext();
private org.springframework.context.ApplicationContext context;
private Environment environment;
private ApplicationContext() {
}
public static ApplicationContext getInstance() {
return INSTANCE;
}
public org.springframework.context.ApplicationContext getContext() {
return context;
}
public void setContext(org.springframework.context.ApplicationContext context) {
this.context = context;
}
public Environment getEnvironment() {
return environment;
}
public void setEnvironment(Environment environment) {
this.environment = environment;
}
public T getBean(String name) {
if (context == null) {
return null;
}
Object bean = context.getBean(name);
if (bean == null) {
return null;
}
return (T) bean;
}
public T getBean(Class type) {
if (context == null) {
return null;
}
Object bean = context.getBean(type);
if (bean == null) {
return null;
}
return (T) bean;
}
public Map getBeansOfType(Class clazz) {
return context.getBeansOfType(clazz);
}
}
package me.ocheng.common.interceptor;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public abstract class AbstractInterceptor extends HandlerInterceptorAdapter {
public abstract boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception;
public abstract void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable ModelAndView modelAndView) throws Exception;
public abstract void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable Exception ex) throws Exception;
public abstract void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception;
}