在 Spring 中结合 GET 和 POST 请求方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17987380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Combine GET and POST request methods in Spring
提问by Prabhakaran Ramaswamy
I have a resource that supports both GETand POSTrequests. Here a sample code for a sample resource:
我有一个同时支持GET和POST请求的资源。这是示例资源的示例代码:
@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 ifinside. I tried, but it doesn't work, because the two methods have a different parameter at the end, i.e. HttpServletRequestand BindingResult(the @RequestParam's are not required and therefore not needed in the POSTrequest). Any ideas how to combine the two methods?
这两种方法中的代码实际上是相同的,除了让我们说一个变量定义。这两种方法可以很容易地结合使用method = {RequestMethod.POST, RequestMethod.GET},并且if里面很简单。我试过了,但它不起作用,因为这两种方法最后有一个不同的参数,即HttpServletRequest和BindingResult(@RequestParam's 不是必需的,因此在POST请求中不需要)。任何想法如何结合这两种方法?
回答by Prabhakaran Ramaswamy
@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
如果@RequestParam(required = true)那么你必须传递参数1,参数2
Use BindingResult and request them based on your conditions.
使用 BindingResult 并根据您的条件请求它们。
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
}
回答by Jayesh
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.
有一种方法接受两种类型的请求,然后检查您收到的请求类型,是“GET”还是“POST”类型,一旦您知道这一点,请执行相应的操作并调用一个为以下对象执行常见任务的方法两种请求方法,即 GET 和 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....);
}
回答by MPPNBD
@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.
这将适用于 GET 和 POST。
For GET if your pojo(BooksFilter) have to contain the attribute which you're using in request parameter
对于 GET,如果您的 pojo(BooksFilter) 必须包含您在请求参数中使用的属性
like below
像下面
public class BooksFilter{
private String parameter1;
private String parameter2;
//getters and setters
URl should be like below
URl 应该如下所示
/books?parameter1=blah
/books?parameter1=blah
Like this way u can use it for both GET and POST
像这样你可以将它用于 GET 和 POST

