1

In a Spring REST controller, is it possible to have a handler method based on a request parameter value?

For example I want the handler method to be invoked only when the value of the type parameter is product.

http://localhost:8080/api/vi/catalog?type=product
2
  • It's possible if you add a if (param.equals("product") {...} in the method. Or are you referring to have a method just with that parameter? Commented Dec 27, 2018 at 2:43
  • You want to have different controller methods for different paramter value??,you can have different methods for different param like http://localhost:8080/api/vi/catalog?type=product can have method A and http://localhost:8080/api/vi/catalog?ty=product can have method b` Commented Dec 27, 2018 at 2:48

3 Answers 3

4

This can be implemented in Spring MVC with the params attribute of @RequestMapping, e.g.

@GetMapping(value = "/catalog", params = "type=product")
public List<Product> getProducts() {
  ...
}

You can also do the inverse, i.e. params = "type!=product" to match all other types.

Sign up to request clarification or add additional context in comments.

Comments

2

Spring only matches request based on path and query parameters.

I think in your case you should try another approach, for example: /api/v1/catalog/product is more reasonable in this case.

Comments

0

I think best approach is to bind a HandlerInterceptor to the path "/api/vi/catalog". Here is an example Interceptor returns 404 response if parameter is not set to product:

@Component
public class ProductInterceptorHandler implements HandlerInterceptor {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    if (!request.getParameter("type").equalsIgnoreCase("product")){
        response.setStatus(404);
        // DO WHATEVER YOU WANT TO INFORM USER THAT IS INVALID
        return false;
    }
    return true;
}

}

And you should bind it to the path inside your configuration class (in my case a SpringBootApplication that extends WebMvcConfigurerAdapter):

@SpringBootApplication

public class ASpringApplication extends WebMvcConfigurerAdapter implements CommandLineRunner {

public static void main(String[] args) {
    SpringApplication.run(ASpringApplication.class, args);
}



@Override
public void addInterceptors(InterceptorRegistry registry){
    registry.addInterceptor(new ProductInterceptorHandler()).addPathPatterns("/api/vi/catalog");
}
}

And finally a restcontroller method:

@RequestMapping(value = "/api/vi/catalog", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> searchLocationIndexed(@RequestParam("type") String type ) throws Exception {

    return ResponseEntity.ok("ok");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.