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(); } }