extends WebMvcConfigurationSupport 和 implements WebMvcConfigurer之间的区别


事情是这样的,springboot项目中原有一个类是 implements WebMvcConfigurer这样的

/**************************************************************************
 * Copyright©2004-2016 浙江盘石信息技术股份有限公司
 * All rights reserved.
 *
 * 项目名称:互金网络平台
 * 版权说明:本软件属浙江盘石信息技术股份有限公司所有,在未获浙江盘石信息技术股份有限公司正式授权情况下,
 *          任何企业和个人,不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。
 ***************************************************************************/
package com.davidhu.shopguide.web.config;

//import javax.annotation.Resource;

//import com.davidhu.shopguide.web.interceptor.CorsInterceptor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;


/**
 * @author <a href="hugaoxiang8619@adpanshi.com">hugaoxiang</a>
 * @version V1.0.1
 * @Description
 * @date 2019年5月9日 下午7:16:15
 */
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {


//    @Resource
//    private CorsInterceptor corsInterceptor;

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//
//        registry.addInterceptor(corsInterceptor).addPathPatterns("/**");
//
//
//    }


//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/api/doc/**").addResourceLocations("/api/doc/");
//    }

    @Override
    public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) {
        // Turn off suffix-based content negotiation
        configurer.favorPathExtension(false);
    }


    @Bean
    public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
        RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();

        List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();

        MappingJackson2XmlHttpMessageConverter xmlConverter = new MappingJackson2XmlHttpMessageConverter();

        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();

        List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();

        MediaType jsonMedis = new MediaType(MediaType.APPLICATION_JSON, Charset.forName("UTF-8"));

        supportedMediaTypes.add(jsonMedis);

        jsonConverter.setSupportedMediaTypes(supportedMediaTypes);
        converters.add(jsonConverter);
        supportedMediaTypes = new ArrayList<MediaType>();
        MediaType xmlMedia = new MediaType(MediaType.TEXT_XML, Charset.forName("UTF-8"));
        supportedMediaTypes.add(xmlMedia);

        xmlConverter.setSupportedMediaTypes(supportedMediaTypes);

        converters.add(xmlConverter);

        adapter.setMessageConverters(converters);

        return adapter;
    }


    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));

        Jackson2ObjectMapperFactoryBean factory = new Jackson2ObjectMapperFactoryBean();
        factory.setSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        factory.afterPropertiesSet();
        MappingJackson2HttpMessageConverter jackson2Converter = new MappingJackson2HttpMessageConverter();
        jackson2Converter.setObjectMapper(factory.getObject());
        converters.add(jackson2Converter);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/**").allowedOriginPatterns("*").allowedMethods("*").allowCredentials(true).allowedHeaders("*");
    }
}

因为java后端的Long型数据返回前端时,js的数字会丢失精度,于是在configureMessageConverters中加上一个MappingJackson2HttpMessageConverter

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

//解决Long型在js里精度丢失
        MappingJackson2HttpMessageConverter jackson2Converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        jackson2Converter.setObjectMapper(objectMapper);

        converters.add(jackson2Converter);
        converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));

    }

但问题总是没有生效,看到网上有人用 extends WebMvcConfigurationSupport ,结果改了之后原先没有的跨域问题却出现了。addCorsMappings不生效了。

后来在github上有人讨论这个问题:https://stackoverflow.com/questions/17898606/difference-between-webmvcconfigurationsupport-and-webmvcconfigureradap

 

The answer is in the doc you referenced above:

If the customization options of WebMvcConfigurer do not expose something you need to configure, consider removing the @EnableWebMvc annotation and extending directly from WebMvcConfigurationSupport overriding selected @Bean methods

In short, if @EnableWebMvc works for you, there is no need to look any further.

也就是 @EnableWebMvc 会做很多默认设置,要自定义的话需要把@EnableWebMvc去掉然后extends WebMvcConfigurationSupport,我这里虽然改成了WebMvcConfigurationSupport,但没有把 @EnableWebMvc去掉所以跨域不生效了。

 

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/270674.html

(0)
上一篇 2022年6月29日
下一篇 2022年6月29日

相关推荐

发表回复

登录后才能评论