@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; } }
原创文章,作者:,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/271446.html