1 - 在springboot中,@RestController 相當於 @Controller + @ResponseBody;
2 - 即在Controller類中,若想返回jsp或html頁面,則不能用@RestController,只能使用@Controller;
3 - 若返回的是json或xml數據,可以有兩種寫法:
1. @RestController注解,然后直接return json數據即可;
2. @Controller注解放類之前,然后若類中某個方法需要返回json數據,則需在該方法前添加@ResponseBody注解;
【示例】:
@RestController public class BookController { @Autowired private BookRepository bookRepository; /** * 返回所有書籍的信息 * @return */ @RequestMapping(value = "/books", method = RequestMethod.GET) public List<Book> bookList(){ return bookRepository.findAll(); } }
@Controller public class BookController2 { @Autowired private BookRepository bookRepository; /** * 返回所有書籍的信息 * @return */ @RequestMapping(value = "/books", method = RequestMethod.GET) @ResponseBody public List<Book> bookList(){ return bookRepository.findAll(); } }
