SpringBoot difference in the @RestController and @Controller

1 - In springboot, the @RestController equivalent ; 2 - that is, in class, if you want to return or page, can not be used , use only ; 3 - if the returns or data may be written in two: @Controller + @ResponseBody
Controllerjsphtml@RestController@Controller
jsonxml

1. @RestController annotation data can then directly return json; 
before liberation Note 2. @Controller class and if the class requires a method to return json data, before the need to add annotations @ResponseBody the method;

Example]:

Copy the code
@RestController
public class BookController {
    @Autowired
    private BookRepository bookRepository;
    
    /** 
     * 返回所有书籍的信息
     * @return
     */
    @RequestMapping(value = "/books", method = RequestMethod.GET)
    public List<Book> bookList(){
        return bookRepository.findAll();
    }
}
Copy the code
Copy the code
@Controller
public class BookController2 {
    @Autowired
    private BookRepository bookRepository;
    
    /** 
     * 返回所有书籍的信息
     * @return
     */
    @RequestMapping(value = "/books", method = RequestMethod.GET)
    @ResponseBody
    public List<Book> bookList(){
        return bookRepository.findAll();
    }
}
Copy the code

Guess you like

Origin www.cnblogs.com/qq1069284034/p/11584970.html