一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Springmvc 4.x利用@ResponseBody返回Json數(shù)據(jù)的方法

Springmvc 4.x利用@ResponseBody返回Json數(shù)據(jù)的方法

2021-04-22 11:07涵涵YH Java教程

這篇文章主要介紹了Springmvc 4.x利用@ResponseBody返回Json數(shù)據(jù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

下面是官方文檔對于@responsebody注解的解釋:

?
1
2
3
4
5
6
7
8
9
10
11
12
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 就會(huì)返回空白。

怎么配置呢 ?需要配置messageconverter:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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>

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

Springmvc 4.x利用@ResponseBody返回Json數(shù)據(jù)的方法

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

Springmvc 4.x利用@ResponseBody返回Json數(shù)據(jù)的方法

控制器代碼:

?
1
2
3
4
5
6
7
@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代碼:

?
1
2
3
4
5
6
7
8
9
//|增加
    $("#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)行返回例子:

Springmvc 4.x利用@ResponseBody返回Json數(shù)據(jù)的方法

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/u012997311/article/details/51982461

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 美国大片成人性网 | 青草草在线观看 | 小莹的性荡生活45章 | 四虎免费在线观看视频 | 亚洲va欧美va国产va天堂影 | 国产精品视频1区 | 亚洲欧美日韩另类精品一区二区三区 | 日韩精品视频在线播放 | 成年人黄色录像 | 亚洲国产精品久久无套麻豆 | 天天av天天翘天天综合网 | 国产成人精品午夜免费 | 9966久久精品免费看国产 | 日本精品一区二区三区 | 日本护士撒尿xxxx欧美 | 日韩欧美亚洲一区二区综合 | jazz中国在线视频 | 久久国产精品无码视欧美 | 韩日视频在线 | 国产在线观看精品香蕉v区 国产在线观看a | 我的绝色岳每雯雯 | 日日精品| 国产精品亚洲午夜一区二区三区 | 日韩毛片免费在线观看 | 国产第一自拍 | 亚洲国产在线视频中文字 | 媳妇和公公小说 | 日本中文字幕一区二区高清在线 | 色综合精品 | 欧美性videossex丝袜 | 免费观看欧美成人h | 91香蕉视频在线观看 | 996免费视频国产在线播放 | 古代翁熄乩伦小说h | 欧美亚洲国产精品久久久 | 高清欧美不卡一区二区三区 | 国产精品亚洲片在线va | 亚洲国产精品热久久 | 天堂在线中文无弹窗全文阅读 | 国产亚洲精品自在线亚洲情侣 | 精品在线免费播放 |