@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; } }