package com.dcit.danger;


import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import com.dcit.common.utils.MyJsonMapper;



@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
	
	/**
	 * 文件访问路径
	 */
	@Value("${projectpath.file-url-path}")
	private String fileUrlPath;
	
	/**
	 * 文件存储跟路径
	 */
	@Value("${projectpath.file-root-path}")
	private String fileRootPath;

	@Override
	protected void addResourceHandlers(ResourceHandlerRegistry registry) {
		// TODO Auto-generated method stub
		 registry.addResourceHandler("swagger-ui.html")
         .addResourceLocations("classpath:/META-INF/resources/");
		 registry.addResourceHandler("/webjars/**")
         .addResourceLocations("classpath:/META-INF/resources/webjars/");
		 registry.addResourceHandler("/statics/**")
         .addResourceLocations("classpath:/statics/");
		 registry.addResourceHandler("/static/**")
         .addResourceLocations("classpath:/static/");
		 registry.addResourceHandler(fileUrlPath+"**").addResourceLocations("file:" + fileRootPath);
		super.addResourceHandlers(registry);
	}
	
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		//在json转换之前先进行string转换
		converters.add(new StringHttpMessageConverter());
		//添加json转换
		MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
		jackson2HttpMessageConverter.setObjectMapper(new MyJsonMapper());
		converters.add(jackson2HttpMessageConverter);
		//5、追加默认转换器
		super.addDefaultHttpMessageConverters(converters);
	}
}
