Spring REST-ful uri with optional querystring

12,842

Solution 1

Try a variation of this:

    @Controller
    @RequestMapping("/cars")
    public clas CarController
    {
        @RequestMapping(method = RequestMethod.get)
        public final @ResponseBody String carsHandler(
            final WebRequest webRequest)
        {
            String parameter = webRequest.getParameter("blammy");

            if (parameter == null)
            {
                return getAll();
            }
            else
            {
                return findCar(webRequest);
            }
        }
    }

Solution 2

You can use

@RequestMapping(method = RequestMethod.GET, params = {/* string array of params required */})
public final @ResponseBody String find(@RequestParam(value = "reg") String reg, @RequestParam(value = "model") String model)
    // logic
}

ie, the @RequestMapping annotation has a property called params. If all of the parameters that you specify are contained in your request (and all other RequestMapping requirements match), then that method will be called.

Share:
12,842

Related videos on Youtube

user2170710
Author by

user2170710

Updated on September 15, 2022

Comments

  • user2170710
    user2170710 over 1 year

    The normal uri which triggers the default controller to get all cars is just "/cars"

    I want to be able to search for cars aswell with an uri, for example: "/cars?model=xyz" which would return a list of matching cars. All the request parameters should be optional.

    The problem is that even with the querystring the default controller triggers anyway and I always get "all cars: ..."

    Is there a way to do this with Spring without a separate search uri (like "/cars/search?..")?

    code:

    @Controller
    @RequestMapping("/cars")
    public class CarController {
    @Autowired
    private CarDao carDao;
    
    @RequestMapping(method = RequestMethod.GET, value = "?")
    public final @ResponseBody String find(
            @RequestParam(value = "reg", required = false) String reg,
            @RequestParam(value = "model", required = false) String model
            )
    {
        Car searchForCar = new Car();
        searchForCar.setModel(model);
        searchForCar.setReg(reg);
        return "found: " + carDao.findCar(searchForCar).toString();
    }
    
    @RequestMapping(method = RequestMethod.GET)
    public final @ResponseBody String getAll() {
        return "all cars: " + carDao.getAllCars().toString();
    } 
    }
    
  • ach
    ach about 11 years
    I prefer using @RequestParam-annotated parameters with required=false for this type of thing.
  • user2170710
    user2170710 about 11 years
    This one seems more elegant since I don't want to have a separate method for each variation of parameters.
  • emkays
    emkays about 8 years
    Thanks it also resolved my problem by using combination of params = {/* mandatory params */} as well as @RequestParam(value = "data", required = false, defaultValue="")