2

Requirement is to write a controller to handle POST requests from the below urls:

http://hostname:port/com/prod1?id=2&action=add
http://hostname:port/com/prod1?id=2&action=minus

http://hostname:port/com/prod2?id=2&action=add
http://hostname:port/com/prod2?id=2&action=minus

Can I have two methods, one for mapping urls with action=add, and another for urls with action=minus? All the requests are POST.

4 Answers 4

6

A couple years between question and answer, but yes it is possible to route based on query parameters with Spring MVC:

@RequestMapping(path = "/com/{product}", params = "add")
String add(@PathVariable("product") String product) {
    System.out.println("add method; product=" + product);
    return "add";
}

@RequestMapping(path = "/com/{product}", params = "minus")
String minus(@PathVariable("product") String product) {
    System.out.println("minus method; product=" + product);
    return "minus";
}
Sign up to request clarification or add additional context in comments.

Comments

1

No to my knowledge. According to your url end points,

they are,

  1. /com/prod1
  2. /com/prod2

For these you can have 2 controller methods for each of these.

EDIT:

If I understand you correctly,

Instead of having the above end points, write 2 controller methods for request mappings,

  1. com/add
  2. com/minus

If you want to have it in a more granular manner, then

  1. com/prod1/add
  2. com/prod1/minus
  3. com/prod2/add
  4. com/prod2/minus

write request mapping methods for the above.

Then you have,

http://hostname:port/com/prod1/add?id=2 http://hostname:port/com/prod1/minus?id=2

http://hostname:port/com/prod2/add?id=2 http://hostname:port/com/prod2/munus?id=2

Or you can use another approach. which is, Just use the generic end points and depending on your request parameters, redirect to different request mappings such as,

  1. com/add
  2. com/minus

2 Comments

I want it based on the action (action=add or action=minus), not based on prod1 or prod2.
@PraveeshP let me know if you need any more help
1

You cannot have two @RequestMapping methods to map to two URLs differing only by query parameters. The @RequestMapping only binds to the path portion of the URL.

You can have two methods if you dispatch on the action value, calling one method for add and another for minus.

Or, you can make add or minus a part of the URL path.

Comments

-1

It is possible with the following code:

@GetMapping(path = "/com/{product}", params = "action=add")
public String add(@PathVariable("product") String product) {
    System.out.println("add method; product=" + product);
    return "add";
}

@GetMapping(path = "/com/{product}", params = "action=minus")
public String minus(@PathVariable("product") String product) {
    System.out.println("minus method; product=" + product);
    return "minus";
}

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.