1. 程式人生 > >SpringBoot @RequestBody 報錯 ('application/x-www-form-urlencoded;charset=UTF-8' not supported)

SpringBoot @RequestBody 報錯 ('application/x-www-form-urlencoded;charset=UTF-8' not supported)

在Spring boot 中使用 @RequestBody 會報錯,提示錯誤 Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported,程式碼如下:

    @RequestMapping(value = "/act/service/model/{modelId}/save", method = RequestMethod.POST)
	public void saveModel(@PathVariable String modelId, @RequestBody MultiValueMap<String, String> values) {
        // 具體程式碼
	}

這個在傳統 spring MVC 中是有效的,但是在 Spring boot 中會報錯。 傳統是 Spring MVC 有效,是因為有 <mvc:annotation-driven> 註解,查資料,<mvc:annotation-driven> 註解配置瞭如下的內容 spring 3.1 版本:

<!-- 註解請求對映  -->
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">        
        <property name="interceptors">
            <list>  
                <ref bean="logNDCInteceptor"/>   <!-- 日誌攔截器,這是你自定義的攔截器 -->
            </list>        
        </property>        
    </bean>      
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">  
            <list>  
                <ref bean="byteArray_hmc" />  
                <ref bean="string_hmc" />  
                <ref bean="resource_hmc" />  
                <ref bean="source_hmc" />  
                <ref bean="xmlAwareForm_hmc" />  
                <ref bean="jaxb2RootElement_hmc" />  
                <ref bean="jackson_hmc" />  
            </list>  
        </property>  
    </bean>  
    <bean id="byteArray_hmc" class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /><!-- 處理.. -->
    <bean id="string_hmc" class="org.springframework.http.converter.StringHttpMessageConverter" /><!-- 處理.. -->
    <bean id="resource_hmc" class="org.springframework.http.converter.ResourceHttpMessageConverter" /><!-- 處理.. -->
    <bean id="source_hmc" class="org.springframework.http.converter.xml.SourceHttpMessageConverter" /><!-- 處理.. -->
    <bean id="xmlAwareForm_hmc" class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" /><!-- 處理.. -->
    <bean id="jaxb2RootElement_hmc" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" /><!-- 處理.. -->
    <bean id="jackson_hmc" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /><!-- 處理json-->

轉載:http://elf8848.iteye.com/blog/875830 這個找到的資料是 3.1 的,但是women可以看到,最後一個配置了 Jackson 的 json 處理程式,在更新的版本中,AnnotationMethodHandlerAdapter 已經廢棄,使用的是 RequestMappingHandlerAdapter,看下 RequestMappingHandlerAdapter 的原始碼。


	public RequestMappingHandlerAdapter() {
		StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
		stringHttpMessageConverter.setWriteAcceptCharset(false);  // see SPR-7316

		this.messageConverters = new ArrayList<HttpMessageConverter<?>>(4);
		this.messageConverters.add(new ByteArrayHttpMessageConverter());
		this.messageConverters.add(stringHttpMessageConverter);
		this.messageConverters.add(new SourceHttpMessageConverter<Source>());
		this.messageConverters.add(new AllEncompassingFormHttpMessageConverter());
	}

這裡面沒有了 json 的處理過程,我們把它加上

@EnableWebMvc
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    
    @Bean
    public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
        RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();
        
        List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();

        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
        List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
        MediaType textMedia = new MediaType(MediaType.TEXT_PLAIN, Charset.forName("UTF-8"));
        supportedMediaTypes.add(textMedia);
        MediaType jsonMedia = new MediaType(MediaType.APPLICATION_JSON, Charset.forName("UTF-8"));
        supportedMediaTypes.add(jsonMedia);jsonConverter.setSupportedMediaTypes(supportedMediaTypes);
        
        converters.add(jsonConverter);
        
        
        adapter.setMessageConverters(converters);
        
       return adapter;
    }
}

成功,報錯消除,正確獲取到了引數