• Controller中返回数据总结(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区别

  • 相关阅读:
    Android ListView常用用法
    android ListView详解
    /使用匿名内部类来复写Handler当中的handlerMessage()方法
    android Handler的使用(一)
    Android之Handler用法总结
    动态设置android:drawableLeft|Right|Top|Bottom
    Android Drawable Resource学习(十)、ScaleDrawable
    Android开发——关于onCreate的解读
    onCreate()方法中的参数Bundle savedInstanceState 的意义用法
    Android之drawable state各个属性详解
  • 原文地址:https://www.cnblogs.com/Jason-Xiang/p/10244075.html
Copyright © 2020-2023  润新知