其他分享
首页 > 其他分享> > Spring boot Controller的使用

Spring boot Controller的使用

作者:互联网

@Controller
public class BookController {
  @GetMapping ("/book")
  @ResponseBody
  public Book book() {
    Book book= new Book();
    book.setAuthor("罗贯中");
    book.setName("三国演义");
    book.setPrice(30f);
    book.setPublicationDate(new Date());
    return book;
  }
}

如果需要频繁地用到@ResponseBody 注解,那么可以采用@RestController 组合注解代替@Controller 和@ResponseBody , 代码如下:

@RestController
public class BookController {
  @GetMapping ("/book")
  public Book book() {
    Book book= new Book();
    book.setAuthor("罗贯中");
    book.setName("三国演义");
    book.setPrice(30f);
    book.setPublicationDate(new Date());
    return book;
  }
}

 

标签:Spring,boot,public,ResponseBody,Controller,Book,new,book
来源: https://www.cnblogs.com/ShengLiu/p/16441545.html