日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

[转发]SPRING MVC3.2案例讲解--SPRING MVC3的@ResponseBody和ResponseEntity

發(fā)布時(shí)間:2025/3/20 c/c++ 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转发]SPRING MVC3.2案例讲解--SPRING MVC3的@ResponseBody和ResponseEntity 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在傳統(tǒng)的開發(fā)過程中,我們的控制CONTROLL層通常需要轉(zhuǎn)向一個(gè)JSP視圖;但隨著WEB2.0相關(guān)技術(shù)的崛起,我們很多時(shí)候只需要返回?cái)?shù)據(jù)即可,而不是一個(gè)JSP頁面。

?

SPRING MVC3的@ResponseBody使Controller直接返回?cái)?shù)據(jù),而不是直接指向具體的視圖;同時(shí)通過

MessageConverter和produces(如produces="text/plain;charset=UTF-8")可以返回各種格式的數(shù)據(jù)(XML,json,RSS,TEXT,字節(jié)流等),本章只介紹最簡(jiǎn)單的使用;

見代碼:

@ResponseBody可以直接返回結(jié)果,

而ResponseEntity?可以定義返回的HttpHeaders和HttpStatus,見文章最后的兩張圖

?

[java]?view plaincopy
  • @RequestMapping(value="/response",?method=RequestMethod.GET)??
  • public?class?ResponseController?{??
  • ??
  • //http://127.0.0.1:8010/response/annotation??
  • ????@RequestMapping("/annotation")??
  • ????public?@ResponseBody?String?responseBody()?{??
  • ????????return?"The?String?ResponseBody";??
  • ????}??
  • ??
  • ??
  • ????@RequestMapping("/charset/accept")??
  • ????public?@ResponseBody?String?responseAcceptHeaderCharset()?{??
  • ????????return?"\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01?(\"Hello?world!\"?in?Japanese)";??
  • ????}??
  • ??
  • //http://127.0.0.1:8010/response/charset/produce??
  • ????@RequestMapping(value="/charset/produce",?produces="text/plain;charset=UTF-8")??
  • ????public?@ResponseBody?String?responseProducesConditionCharset()?{??
  • ????????return?"\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01?(\"Hello?world!\"?in?Japanese)";??
  • ????}??
  • ??
  • //http://127.0.0.1:8010/response/entity/status??
  • ????@RequestMapping("/entity/status")??
  • ????public?ResponseEntity<String>?responseEntityStatusCode()?{??
  • ????????return?new?ResponseEntity<String>("The?String?ResponseBody?with?custom?status?code?(403?Forbidden)",??
  • ????????????????HttpStatus.FORBIDDEN);??
  • ????}??
  • ??
  • //http://127.0.0.1:8010/response/entity/headers??
  • ????@RequestMapping("/entity/headers")??
  • ????public?ResponseEntity<String>?responseEntityCustomHeaders()?{??
  • ????????HttpHeaders?headers?=?new?HttpHeaders();??
  • ????????headers.setContentType(MediaType.TEXT_PLAIN);??
  • ????????return?new?ResponseEntity<String>("The?String?ResponseBody?with?custom?header?Content-Type=text/plain",??
  • ????????????????headers,?HttpStatus.OK);??
  • ????}??
  • ??
  • }??
  • ?

    ?

    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/may-25/p/5485948.html

    總結(jié)

    以上是生活随笔為你收集整理的[转发]SPRING MVC3.2案例讲解--SPRING MVC3的@ResponseBody和ResponseEntity的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。