如何在POST方法中指定多个参数

时间:2015-12-24 14:37:42

标签: spring rest model-view-controller spring-boot

我的模型名为贷款:

public class Loan {
    private int loan_id;
    private String clientName;
    private String clientSurname;
    private Double amount;
    private int days;
    //getters and setters
}

和控制器

@RestController
public class MyController {

@Autowired
MyService myService;


@RequestMapping(value = "/makeAction",method = RequestMethod.POST)
public String makeLoan(){
     return myService.makeAction(...);
}

}

问题是:如何通过地址栏绕过多个变量,如:

  

本地主机:8080 / makeAction的LoanID = 1#CLIENTNAME = Stive#clientSurname = Wassabi

等等。

UPD:另一次尝试失败了:

    @RequestMapping(value="/makeLoan",method = RequestMethod.GET)
    public String makeLoan(@PathVariable("loan_id")int loan_id,
                           @PathVariable("name") String clientName,
                           @PathVariable("surname") String clientSurname,
                           @PathVariable("amount") double amount,
                           @PathVariable("days") int days ) throws Exception {
      return myService.makeLoan(loan_id,clientName,clientSurname,amount,days);

P.S尝试@PathVariables - 未能使用

4 个答案:

答案 0 :(得分:3)

谢谢大家帮助我解决这个问题

最终代码如下:

@RequestMapping(value = "/makeAction")
    public String makeLoan(@RequestParam("loan_id")int loan_id,
                           @RequestParam("clientName")String clientName,
                           @RequestParam("clientSurname")String clientSurname,
                           @RequestParam("amount")double amount,
                           @RequestParam("days")int days ) throws Exception {
        return loanService.makeAction(loan_id,clientName,clientSurname,amount,days);
    }

我必须删除GET / POST方法并将@PathVariable切换为@RequestParam

答案 1 :(得分:1)

在您的方法中,使用RequestParam注释定义它们,如:

public String makeLoan(@RequestParam(value="clientName", required=false) String clientName) {//and others, and hope you meant & to seperate request parameters.
}

答案 2 :(得分:1)

首先,您不应该在URL中输入POST参数。

网址参数用于GET,它们用&分隔。所以在你的情况下:

localhost:8080/makeAction?loanId=1&clientName=Stive&clientSurname=Wassabi

对于POST,您应该提交参数作为请求正文参数。参数与@SMA建议的@RequestParam注释绑定。

答案 3 :(得分:0)

好吧,假设您使用的是Spring MVC,这可能会有所帮助: How to explictely obtain post data in Spring MVC?

请注意,如果您正在使用POST方法,则应在请求正文中读取您的参数...