Method 1: Add the produces method to @RequestMapping
@RequestMapping(value = "/upload.do",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
This method has limitations and can only work in one method
Method 2: Perform global configuration
1. First of all, we must rely on the jackson package
<dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency>
2. Global configuration in springmvc
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" > <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=utf-8</value> <value>text/html;charset=UTF-8</value> </list> </property> </bean> </list> </property> </bean> <mvc:annotation-driven/>
The configuration here must be placed in
<mvc:annotation-driven/> above, and the configuration of this mvc must not be less, otherwise an error will be reported