Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 337 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Combine GET and POST request methods in Spring

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

@RequestMapping(value = "/books", method = RequestMethod.GET)
public ModelAndView listBooks(@ModelAttribute("booksFilter") BooksFilter filter, two @RequestParam parameters, HttpServletRequest request)
throws ParseException {
LONG CODE
}


@RequestMapping(value = "/books", method = RequestMethod.POST)
public ModelAndView listBooksPOST(@ModelAttribute("booksFilter") BooksFilter filter, BindingResult result)
throws ParseException {
SAME LONG CODE with a minor difference
}
The code in the two methods is practically the same, except for lets say a variable definition. The two methods can be easily combined using `method = {RequestMethod.POST, RequestMethod.GET}`, and a simple `if` inside. I tried, but it doesn't work, because the two methods have a different parameter at the end, i.e. `HttpServletRequest` and `BindingResult` (the `@RequestParam`'s are not required and therefore not needed in the `POST` request). Any ideas how to combine the two methods?
Reply

#2
@RequestMapping(value = "/testonly", method = { RequestMethod.GET, RequestMethod.POST })
public ModelAndView listBooksPOST(@ModelAttribute("booksFilter") BooksFilter filter,
@RequestParam(required = false) String parameter1,
@RequestParam(required = false) String parameter2,
BindingResult result, HttpServletRequest request)
throws ParseException {

LONG CODE and SAME LONG CODE with a minor difference
}



if `@RequestParam(required = true)` then you must pass parameter1,parameter2

Use BindingResult and request them based on your conditions.


**The Other way**


@RequestMapping(value = "/books", method = RequestMethod.GET)
public ModelAndView listBooks(@ModelAttribute("booksFilter") BooksFilter filter,
two @RequestParam parameters, HttpServletRequest request) throws ParseException {

myMethod();

}


@RequestMapping(value = "/books", method = RequestMethod.POST)
public ModelAndView listBooksPOST(@ModelAttribute("booksFilter") BooksFilter filter,
BindingResult result) throws ParseException {

myMethod();

do here your minor difference
}

private returntype myMethod(){
LONG CODE
}

Reply

#3
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....);
}

Reply

#4
@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
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through