Spring MVC returns JSON data (Spring3.0 MVC+Jackson+AJAX)

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
Frame of this text is: SpringMVC 3.2.3, Jackson 1.9.2

Traditional return to JSON format with AJAX, the general approach is: first the data in the background (Object) package the data into JSON, then HttpServletResponse returns.

In this example, SpringMVC returns can directly support the JSON format data. The details are as follows.

1, JAR package: SPRINGMVC bag to bag, two bags also need JACKSON.
jackson-core-asl-1.9.2.jar
jackson-mapper-asl-1.9.2.jar

2, Spring-servlet.xml add:
<!Returns the JSON template: --> 
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >  
    <property name="messageConverters">  
	<list>
	     <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
	</list> 
    </property>  
</bean>  

We added a new adapter in the SPRING configuration: AnnotationMethodHandlerAdapter, through this adapter, we configure an attribute, messageConverters, which mappingJacksonHttpMessageConverter the Bean, it is used to process the JSON data conversion.
Note: there is no messy code in my projects, such coordination can, if be garbled phenomenon, you can configure the supportedMediaTypes attribute in the MappingJacksonHttpMessageConverter BEAN, is used to solve the problem returns.

3, The use of Controller in the
@Controller
public class SelectController {
	@Resource
	private TypeService typeService;

	@RequestMapping("/type")
	@ResponseBody
	public Object type(){
		List<Type> typelist = this.typeService.getTypeByParentid(Const.TYPE_DAILY);
		return typelist;
	}
}

In the SpringMVC in a Controller plus@ResponseBodyNote, that the method returns the result directly into HTTP response body.

------------------------------------------
The problems encountered:

Access using the above controller,: http://localhost:8080/demo/type.htm, reported 406 wrong.:
Failed to load resource: the server responded with a status of 406 (Not Acceptable) : The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ()

Check the information that, the version of the problem is not JAR, there description:

1, Spring 3.2 requestedMediaTypes [text/html] report 406 errors, there is a reason may be due to the use of the suffix, if you use the *.htm, *.html, the default will be used the [text/html] code, if changed to *.json, *.shtml, OK

2, 3.2.4 also encountered this problem. Modify the Ajax request. JSON or other can. He will be priority according to the URL request suffix determines the type of request. So what you see is always[text/html]

Therefore, the access path from http://localhost:8080/demo/type.htm to http://localhost:8080/demo/type.json.
(if you only interception at the beginning of the HTM link in the web.xml, a new url-pattern *.json servlet. )



-----------------------------------------
Reference resources
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Selma at December 13, 2013 - 11:14 AM