Combine GET and POST request methods in Spring

前端 未结 3 953
醉酒成梦
醉酒成梦 2020-12-07 20:14

I have a resource that supports both GET and POST requests. Here a sample code for a sample resource:

@RequestMapping(value = \"/bo         


        
3条回答
  •  囚心锁ツ
    2020-12-07 20:34

    @RequestMapping(value = "/books", method = { RequestMethod.GET, 
    RequestMethod.POST })
    public ModelAndView listBooks(@ModelAttribute("booksFilter") BooksFilter filter,
         HttpServletRequest request) 
        throws ParseException {
    
    //your code 
    }
    

    This will works for both GET and POST.

    For GET if your pojo(BooksFilter) have to contain the attribute which you're using in request parameter

    like below

    public class BooksFilter{
    
    private String parameter1;
    private String parameter2;
    
       //getters and setters
    

    URl should be like below

    /books?parameter1=blah

    Like this way u can use it for both GET and POST

提交回复
热议问题