国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Springmvc 4.x利用@ResponseBody返回Json數據的方法

2024-07-14 08:40:18
字體:
來源:轉載
供稿:網友

下面是官方文檔對于@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數據返回給瀏覽器!如果為 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> 

下面貼出在官方文檔中的位置:

Springmvc4,@ResponseBody.Json,spring,mvc,返回json

這個需要jackson jar包支持,需要 jackson-annotations,jackson-core,jackson-databind三個包,:

Springmvc4,@ResponseBody.Json,spring,mvc,返回json

控制器代碼:

@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);       });     }); 

運行返回例子:

Springmvc4,@ResponseBody.Json,spring,mvc,返回json

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吉安市| 英超| 汉川市| 仁寿县| 淮北市| 濮阳县| 西和县| 白河县| 阳朔县| 香港| 甘泉县| 右玉县| 安陆市| 涞水县| 西藏| 雷山县| 普格县| 舞钢市| 百色市| 山丹县| 讷河市| 佳木斯市| 乌海市| 林口县| 伊吾县| 萝北县| 武定县| 南康市| 昌图县| 囊谦县| 辰溪县| 阜南县| 万山特区| 南陵县| 黑山县| 仁寿县| 康定县| 临汾市| 金华市| 左云县| 区。|