Links

  1. package com.example.api.config;
  2. import com.alibaba.fastjson.serializer.SerializerFeature;
  3. import com.alibaba.fastjson.support.config.FastJsonConfig;
  4. import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.http.converter.HttpMessageConverter;
  7. import org.springframework.web.servlet.config.annotation.CorsRegistry;
  8. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  9. import java.util.List;
  10. /**
  11. * WebMvcConfigurerImpl
  12. *
  13. * @author Where
  14. * @date 2019-01-31
  15. */
  16. @Configuration
  17. public class WebMvcConfigurerImpl implements WebMvcConfigurer {
  18. /**
  19. * 配置 FastJson
  20. *
  21. * @param converters
  22. */
  23. @Override
  24. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  25. FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
  26. FastJsonConfig config = new FastJsonConfig();
  27. config.setSerializerFeatures(
  28. SerializerFeature.WriteMapNullValue
  29. );
  30. converter.setFastJsonConfig(config);
  31. converters.add(0, converter);
  32. }
  33. /**
  34. * 配置跨域请求
  35. *
  36. * @param registry
  37. */
  38. @Override
  39. public void addCorsMappings(CorsRegistry registry) {
  40. registry.addMapping("/**")
  41. .allowedOrigins("*")
  42. .allowedMethods("*")
  43. .allowedHeaders(("*"))
  44. .allowCredentials(true)
  45. .maxAge(3600L);
  46. }
  47. }