1. 程式人生 > 其它 >Controller中返回資料總結(ResponseEntity,@ResponseBody,@ResponseStatus) SPRING MVC3.2案例講解--SPRING MVC3的@ResponseBody和ResponseEntity

Controller中返回資料總結(ResponseEntity,@ResponseBody,@ResponseStatus) SPRING MVC3.2案例講解--SPRING MVC3的@ResponseBody和ResponseEntity

Controller中返回資料總結(ResponseEntity,@ResponseBody,@ResponseStatus)

https://www.cnblogs.com/Jason-Xiang/p/10244075.html

在傳統的開發過程中,我們的控制CONTROLLER層通常需要轉向一個JSP檢視;但隨著WEB2.0相關技術的崛起,我們很多時候只需要返回資料即可,而不是一個JSP頁面。

  • ResponseEntity:表示整個HTTP響應:狀態程式碼,標題和正文。因此,我們可以使用它來完全配置HTTP響應,它是一個物件。
  • @ResponseBody:返回json格式的結果
  • @ResponseStatus:返回狀態

 

ResponseEntity

ResponseEntity是一種泛型型別。因此,我們可以使用任何型別作為響應主體:

@Controller
public class XXXController{

@GetMapping("/hello")
public ResponseEntity<String> hello() {
return new ResponseEntity<>("Hello World!", HttpStatus.OK);
}

這裡字串"Hello World!"作為字串返回給REST端。

我們可以設定HTTP標頭:

@GetMapping("/customHeader")
ResponseEntity
<String> customHeader() { HttpHeaders headers = new HttpHeaders(); headers.add("Custom-Header", "foo");

return new ResponseEntity<>(
"Custom header set", headers, HttpStatus.OK);
}

設定自定義標頭:

@GetMapping("/customHeader")
ResponseEntity<String> customHeader() {
   return ResponseEntity.ok()
         .header(
"Custom-Header", "foo") .body("Custom header set")

如果將一個物件放入:

@GetMapping("/hello")
 public ResponseEntity<String> hello() {
   return new ResponseEntity<>(new User(‘jdon’), HttpStatus.OK);
 }

返回的是JSON字串:

[ { ‘name’: 'jdon'}]

下面是返回物件的JSON列表:

public ResponseEntity<List<ProcessDef>> repositoryProcessDefinitionsGet() {
   return new ResponseEntity<>(processDefRepo.findAll(), HttpStatus.FOUND);
}

以上是通過ResponseEntity這個物件在程式碼中靈活操控響應,但是在一般情況下我們只是想返回一個帶有資料的正常響應,那麼只要使用@註解即可

 

@ResponseBody

在類級別使用@Controller標註情況下, @ResponseBody註解告訴返回的物件將自動序列化為JSON,並通過回控制器的HttpResponse物件。

@Controller
public class XXXController{

@ResponseBody
public User postResponseController(@RequestBody LoginForm loginForm) {
return new User("Thanks For Posting!!!");
}

將返回客戶端JSON字串:

[ { ‘name’: Thanks For Posting!!!"}]

在@RestController註解了類的情況下,我們就不需要再使用@ResponseBody了。

 

@ResponseStatus

ResponseStatus雖然只是規定了返回的狀態,但是隻需要標註在方法上,簡單,而且狀態碼與返回型別分離,比較清晰。我們將上面返回物件列表的程式碼使用ResponseStatus改寫如下,注意類級別@RestController:

@RestController
public class XXXController{

@ResponseStatus(HttpStatus.FOUND)
public User postResponseController() {
return new User("Thanks For Posting!!!");
}

這也會返回客戶端JSON字串:

[ { ‘name’: Thanks For Posting!!!"}]

這樣的程式碼更加專注於業務。

 

直接操控響應

Spring還允許我們直接訪問javax.servlet.http.HttpServletResponse物件; 我們只需要將它宣告為方法引數:

@GetMapping("/manual")
public void manual(HttpServletResponse response) throws IOException {
      response.setHeader("Custom-Header", "foo");
      response.setStatus(200);
      response.getWriter().println("Hello World!");
      }

由於Spring在底層實現之上提供了抽象和附加功能,因此如果以這種方式直接操縱響應,會失去很多Spring提供方便功能。

 

 

 

 

參考:

SPRING MVC3.2案例講解--SPRING MVC3的@ResponseBody和ResponseEntity

ResponseEntity和@ResponseBody以及@ResponseStatus區別

 

 

在傳統的開發過程中,我們的控制CONTROLLER層通常需要轉向一個JSP檢視;但隨著WEB2.0相關技術的崛起,我們很多時候只需要返回資料即可,而不是一個JSP頁面。

  • ResponseEntity:表示整個HTTP響應:狀態程式碼,標題和正文。因此,我們可以使用它來完全配置HTTP響應,它是一個物件。
  • @ResponseBody:返回json格式的結果
  • @ResponseStatus:返回狀態

 

ResponseEntity

ResponseEntity是一種泛型型別。因此,我們可以使用任何型別作為響應主體:

@Controller
public class XXXController{

@GetMapping("/hello")
public ResponseEntity<String> hello() {
return new ResponseEntity<>("Hello World!", HttpStatus.OK);
}

這裡字串"Hello World!"作為字串返回給REST端。

我們可以設定HTTP標頭:

@GetMapping("/customHeader")
ResponseEntity<String> customHeader() {
   HttpHeaders headers = new HttpHeaders();
   headers.add("Custom-Header", "foo");

return new ResponseEntity<>(
"Custom header set", headers, HttpStatus.OK);
}

設定自定義標頭:

@GetMapping("/customHeader")
ResponseEntity<String> customHeader() {
   return ResponseEntity.ok()
         .header("Custom-Header", "foo")
         .body("Custom header set")

如果將一個物件放入:

@GetMapping("/hello")
 public ResponseEntity<String> hello() {
   return new ResponseEntity<>(new User(‘jdon’), HttpStatus.OK);
 }

返回的是JSON字串:

[ { ‘name’: 'jdon'}]

下面是返回物件的JSON列表:

public ResponseEntity<List<ProcessDef>> repositoryProcessDefinitionsGet() {
   return new ResponseEntity<>(processDefRepo.findAll(), HttpStatus.FOUND);
}

以上是通過ResponseEntity這個物件在程式碼中靈活操控響應,但是在一般情況下我們只是想返回一個帶有資料的正常響應,那麼只要使用@註解即可

 

@ResponseBody

在類級別使用@Controller標註情況下, @ResponseBody註解告訴返回的物件將自動序列化為JSON,並通過回控制器的HttpResponse物件。

@Controller
public class XXXController{

@ResponseBody
public User postResponseController(@RequestBody LoginForm loginForm) {
return new User("Thanks For Posting!!!");
}

將返回客戶端JSON字串:

[ { ‘name’: Thanks For Posting!!!"}]

在@RestController註解了類的情況下,我們就不需要再使用@ResponseBody了。

 

@ResponseStatus

ResponseStatus雖然只是規定了返回的狀態,但是隻需要標註在方法上,簡單,而且狀態碼與返回型別分離,比較清晰。我們將上面返回物件列表的程式碼使用ResponseStatus改寫如下,注意類級別@RestController:

@RestController
public class XXXController{

@ResponseStatus(HttpStatus.FOUND)
public User postResponseController() {
return new User("Thanks For Posting!!!");
}

這也會返回客戶端JSON字串:

[ { ‘name’: Thanks For Posting!!!"}]

這樣的程式碼更加專注於業務。

 

直接操控響應

Spring還允許我們直接訪問javax.servlet.http.HttpServletResponse物件; 我們只需要將它宣告為方法引數:

@GetMapping("/manual")
public void manual(HttpServletResponse response) throws IOException {
      response.setHeader("Custom-Header", "foo");
      response.setStatus(200);
      response.getWriter().println("Hello World!");
      }

由於Spring在底層實現之上提供了抽象和附加功能,因此如果以這種方式直接操縱響應,會失去很多Spring提供方便功能。

 

 

 

 

參考:

SPRING MVC3.2案例講解--SPRING MVC3的@ResponseBody和ResponseEntity

ResponseEntity和@ResponseBody以及@ResponseStatus區別