Springmvc 4.x利用@ResponseBody返回Json數(shù)據(jù)的方法
下面是官方文檔對于@ResponseBody注解的解釋:
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.
@ResopnseBody注解能夠 直接把 控制器返回變量(String)直接 返回給瀏覽器,也可以通過配置 后,把 對象 序列化成Json數(shù)據(jù)返回給瀏覽器!如果為 null 就會返回空白。
怎么配置呢 ?需要配置MessageConverter:
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJackson2HttpMessageConverter" /> </list> </property> </bean> <bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.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>
下面貼出在官方文檔中的位置:
這個需要jackson jar包支持,需要 jackson-annotations,jackson-core,jackson-databind三個包,:
控制器代碼:
@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代碼:
//|增加 $("#hanblog_add_btn").click(function(){ var classname=$("#add_input_name").val(); var classintroduction=$("#add_input_introduction").val(); alert("分類名稱:"+classname+"分類介紹:"+classintroduction); $.get("<c:url value="/House/ClassManager/addByAjax.do" />",function(result){ alert(result); }); });
運(yùn)行返回例子:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解OpenAPI開發(fā)如何動態(tài)的添加接口實(shí)現(xiàn)
這篇文章主要為大家介紹了OpenAPI開發(fā)如何動態(tài)的添加接口實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04為什么mybatis中的SqlSession一定要關(guān)閉
這篇文章主要介紹了為什么mybatis中的SqlSession一定要關(guān)閉,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12IntelliJ IDEA下載GitHub私有倉庫到本地的方法(新版)
這篇文章主要介紹了IntelliJ IDEA下載GitHub私有倉庫到本地(新版),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05springboot項目(jar包)指定配置文件啟動圖文教程
這篇文章主要給大家介紹了關(guān)于springboot項目(jar包)指定配置文件啟動的相關(guān)資料,在多環(huán)境部署過程中、及線上運(yùn)維中可能會遇到臨時指定配置文件的情況,需要的朋友可以參考下2023-07-07MybatisPlus EntityWrapper如何自定義SQL
這篇文章主要介紹了MybatisPlus EntityWrapper如何自定義SQL,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03ExecutorService Callable Future多線程返回結(jié)果原理解析
這篇文章主要為大家介紹了ExecutorService Callable Future多線程返回結(jié)果,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Java+Springboot搭建一個在線網(wǎng)盤文件分享系統(tǒng)
本主要介紹了通過springboot+freemark+jpa+MySQL實(shí)現(xiàn)的在線網(wǎng)盤文件分享系統(tǒng),其功能跟百度網(wǎng)盤非常類似,可以實(shí)現(xiàn)文件的上傳、移動、復(fù)制、下載等,需要的可以參考一下2021-11-11Java實(shí)現(xiàn)圖片倒影的源碼實(shí)例內(nèi)容
在本篇文章里小編給大家整理的是關(guān)于Java實(shí)現(xiàn)圖片倒影的源碼以及相關(guān)知識點(diǎn),有需要的朋友們學(xué)習(xí)下。2019-09-09從Myeclipse 導(dǎo)入到eclipse中無法識別為 web項目 問題的解決步驟
這篇文章主要介紹了從Myeclipse 導(dǎo)入到eclipse中無法識別為 web項目 問題的解決步驟,需要的朋友可以參考下2018-05-05