1

I have Model called Loan:

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

And Controller

@RestController
public class MyController {

@Autowired
MyService myService;


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

}

The question is: how to bypass multiple variables via adressbar like:

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

and so on.

UPD: Another attempt failed:

    @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 tried @PathVariables - failed to use

Malakai
  • 3,011
  • 9
  • 35
  • 49

4 Answers4

4

Thanks you all for helping me with this

The final code looks like that:

@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);
    }

I had to remove GET/POST method and switch @PathVariable to @RequestParam

Malakai
  • 3,011
  • 9
  • 35
  • 49
3

Well, first of all, you shouldn't put parameters for POST in the URL.

URL parameters are used for GET, and they are separated with & so in your case:

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

For POST you should submit parameters as request body parameters. Parameters are bound with @RequestParam annotation like @SMA suggested.

Vao Tsun
  • 47,234
  • 13
  • 100
  • 132
Milan
  • 1,780
  • 1
  • 16
  • 29
2

In your method define them with RequestParam annotation like:

public String makeLoan(@RequestParam(value="clientName", required=false) String clientName) {//and others, and hope you meant & to seperate request parameters.
}
SMA
  • 36,381
  • 8
  • 49
  • 73
  • 1
    Instead of `@PathVariable` use `@RequestParam` in your updated question and you could set attribute `required` as true for each of them. – SMA Dec 24 '15 at 15:00
1

Well, assuming you're using spring MVC, this could be helpful: How to explictely obtain post data in Spring MVC?

Be aware that if you're using a POST method, your parameters should be read in the request body...

Community
  • 1
  • 1
Jérôme
  • 101
  • 5