Combine GET and POST request methods in Spring

前端 未结 3 964
醉酒成梦
醉酒成梦 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:47

    Below is one of the way by which you can achieve that, may not be an ideal way to do.

    Have one method accepting both types of request, then check what type of request you received, is it of type "GET" or "POST", once you come to know that, do respective actions and the call one method which does common task for both request Methods ie GET and POST.

    @RequestMapping(value = "/books")
    public ModelAndView listBooks(HttpServletRequest request){
         //handle both get and post request here
         // first check request type and do respective actions needed for get and post.
    
        if(GET REQUEST){
    
         //WORK RELATED TO GET
    
        }else if(POST REQUEST){
    
          //WORK RELATED TO POST
    
        }
    
        commonMethod(param1, param2....);
    }
    

提交回复
热议问题