Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle a request with multiple parameters on Spring-MVC

I am sending following request that need to be handled by Spring-MVC but it does not.

http://localhost:2001/MyProject/flights/find?origin=LAX&destination=WA&departure=&arrival=&return=false

Code

@Controller
@RequestMapping("/flights")
public class FlightController {

   @RequestMapping(value = "/find?origin={origin}&destination={destination}&departure={departure}&arrival={arrival}&return={return}", method = RequestMethod.GET)
    public String findFlight(@PathVariable String origin,
            String destination, Date departure, Date arrival, boolean return) {
like image 478
Jack Avatar asked Mar 22 '15 04:03

Jack


1 Answers

That is not the correct way (or place) to use @PathVariable. You need to use @RequestParam.

@Controller
@RequestMapping("/flights")
public class FlightController {
  @RequestMapping("/find")
  public String findFlight(@RequestParam String origin
                          , @RequestParam String destination
                          , @RequestParam(required = false) Date departure
                          , @RequestParam(required = false) Date arrival
                          , @RequestParam(defaultValue = "false", required = false, value = "return") Boolean ret) { ... }
}

Note that return is a keyword in Java so you cannot use it as a method parameter name.

You will also have to add a java.beans.PropertyEditor for reading the dates because the dates will (presumably) be in a specific format.

like image 54
manish Avatar answered Jan 02 '23 02:01

manish