| | |
| | | import com.alibaba.fastjson.support.config.FastJsonConfig; |
| | | import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.http.converter.HttpMessageConverter; |
| | | import org.springframework.web.servlet.config.annotation.CorsRegistry; |
| | | import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; |
| | | |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Configuration |
| | | public class WebConfig extends WebMvcConfigurationSupport { |
| | | /** |
| | | * 跨域请求 |
| | | */ |
| | | @Override |
| | | protected void addCorsMappings(CorsRegistry registry) { |
| | | registry.addMapping("/**") |
| | | //.allowCredentials(true) |
| | | .allowedHeaders("*") |
| | | .allowedOrigins("*") |
| | | .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") |
| | | .allowCredentials(true) |
| | | .maxAge(3600); |
| | | } |
| | | |
| | | /** |
| | | * 处理json格式,值null的转换为"" |
| | | */ |
| | |
| | | // 结果是否格式化,默认为false |
| | | //SerializerFeature.PrettyFormat); |
| | | |
| | | List<MediaType> supportedMediaTypes = new ArrayList<>(); |
| | | supportedMediaTypes.add(MediaType.APPLICATION_JSON); |
| | | // supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8) |
| | | supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML); |
| | | supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED); |
| | | supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM); |
| | | supportedMediaTypes.add(MediaType.APPLICATION_PDF); |
| | | supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML); |
| | | supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML); |
| | | supportedMediaTypes.add(MediaType.APPLICATION_XML); |
| | | supportedMediaTypes.add(MediaType.IMAGE_GIF); |
| | | supportedMediaTypes.add(MediaType.IMAGE_JPEG); |
| | | supportedMediaTypes.add(MediaType.IMAGE_PNG); |
| | | supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM); |
| | | supportedMediaTypes.add(MediaType.TEXT_HTML); |
| | | supportedMediaTypes.add(MediaType.TEXT_MARKDOWN); |
| | | supportedMediaTypes.add(MediaType.TEXT_PLAIN); |
| | | supportedMediaTypes.add(MediaType.TEXT_XML); |
| | | converter.setSupportedMediaTypes(supportedMediaTypes); |
| | | |
| | | converter.setFastJsonConfig(config); |
| | | converter.setDefaultCharset(StandardCharsets.UTF_8); |
| | | converters.add(converter); |