WebMvcConfig.java 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package com.keao.edu.user.config;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.format.FormatterRegistry;
  8. import org.springframework.http.MediaType;
  9. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  10. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  11. import com.keao.edu.common.config.EnumConverterFactory;
  12. import com.keao.edu.common.config.LocalFastJsonHttpMessageConverter;
  13. import com.keao.edu.common.tenant.TenantInterceptor;
  14. import com.keao.edu.datasource.interceptor.DataSourceInterceptor;
  15. @Configuration
  16. public class WebMvcConfig implements WebMvcConfigurer {
  17. /**
  18. * 枚举类的转换器 addConverterFactory
  19. */
  20. @Override
  21. public void addFormatters(FormatterRegistry registry) {
  22. registry.addConverterFactory(new EnumConverterFactory());
  23. }
  24. @Override
  25. public void addInterceptors(InterceptorRegistry registry) {
  26. registry.addInterceptor(getDataSrouceInterceptor()).addPathPatterns("/**");
  27. registry.addInterceptor(getTenantInterceptor()).addPathPatterns("/**");
  28. }
  29. @Bean
  30. public DataSourceInterceptor getDataSrouceInterceptor() {
  31. return new DataSourceInterceptor();
  32. }
  33. @Bean
  34. public TenantInterceptor getTenantInterceptor() {
  35. return new TenantInterceptor();
  36. }
  37. @Bean
  38. public HttpMessageConverters fastJsonHttpMessageConverters(){
  39. LocalFastJsonHttpMessageConverter converter = new LocalFastJsonHttpMessageConverter();
  40. List<MediaType> fastMediaTypes = new ArrayList<MediaType>();
  41. fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
  42. converter.setSupportedMediaTypes(fastMediaTypes);
  43. return new HttpMessageConverters(converter);
  44. }
  45. }