Here is the official documentation's explanation of @ResponseBody annotation:
Mapping the response body with the @ResponseBody annotation The @ResponseBody annotation is similar to @RequestBody. This annotation can be put on a method and indicates that the return type should be written straight to the HTTP response body (and not placed in a Model, or interpreted as a view name). For example: @RequestMapping(path = "/something", method = RequestMethod.PUT) @ResponseBody public String helloWorld() { return "Hello World"; } The above example will result in the text Hello World being written to the HTTP response stream. As with @RequestBody, Spring converts the returned object to a response body by using an HttpMessageConverter. For more information on these converters, see the previous section and Message Converters.The @ResopnseBody annotation can directly return the controller return variable (String) to the browser, or after configuration, it can serialize the object into Json data and return it to the browser! If null, a blank will be returned.
How to configure it? MessageConverter needs to be configured:
<bean > <property name="messageConverters"> <list> <ref bean="mappingJackson2HttpMessageConverter" /> </list> </property> </bean> <bean id="mappingJackson2HttpMessageConverter" > <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>text/json;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean>
The following is the location in the official document:
This requires support from the jackson jar package, and requires three packages:
Controller code:
@RequestMapping("House/ClassManager/addByAjax") @ResponseBody public HanBlog_Class ClassManager_addByAjax(HttpServletRequest request){ if(request.getSession().getAttribute("hanblog_uid")==null) return null; HanBlog_Class objClass=new HanBlog_Class(); return objClass; }jquery code:
//|Add $("#hanblog_add_btn").click(function(){ var classname=$("#add_input_name").val(); var classsource=$("#add_input_introduction").val(); alert("Classification name:"+classname+"Classification introduction:"+classification); $.get("<c:url value="/House/ClassManager/addByAjax.do" />",function(result){ alert(result); }); });Run and return example:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.