1

I don't know this is the right approach but the easiest solution I can come up with in the moment.

I want to use multiple values in @RequestMapping and do the business logic in the method according which value is called the method. Example:

@RequestMapping(value = {"/delete", "/save"}, method = RequestMethod.POST)
public String crudOps(@ModelAttribute ("userForm") User user) {

   // find user in repository....

   if(value is delete) // don't know how to make this check
      delete(user);
   else
      save(user);
}

How can I make that if statement work?

2
  • if you do not provide two methods one for /delete and another for /save you would need to pass another parameter to your post to indicated which to do, such as @RequestParam(value = "delete", required = true) boolean delete, unles you have something in the User object that indicated the operation Commented Mar 7, 2015 at 21:16
  • 1
    I would not recommend to follow this approach. One method should do one thing. In this case you map two completely different actions on one method. This increases cyclomatic complexity. Not that much in this case - but i quite often see approaches like this in complex business logic - making it more complex as it should be. Commented Mar 7, 2015 at 21:55

3 Answers 3

5

fter adding a comment above, I thought of a different solution by accessing the HttpServletRequest getServletPath method,

@RequestMapping(value = {"/delete", "/save"}, method = RequestMethod.POST)
public String crudOps(@ModelAttribute ("userForm") User user, HttpServletRequest request) {

   // find user in repository....

   if(request.getServletPath().equals("/delete"))
      delete(user);
   else
      save(user);
}
Sign up to request clarification or add additional context in comments.

3 Comments

if(action is delete) does not compile, of course.
sorry just updated the answer, we are comparing strings
I was hopping that @iamiddy 's solution would work but I got some wierd issues with it. I am sure my code has some issues but your solution worked like charm... Thank you!
1

You can use @PathVariale to grab part of the URL as follows

@RequestMapping(value = /{action}, method = RequestMethod.POST)
 public String crudOps(@PathVariable String action, @ModelAttribute ("userForm") User user) {

       // find user in repository....

       if(action.equals("delete"))
          delete(user);
       else
          save(user);}

1 Comment

if(action is delete) does not compile, of course.
1

I would personally go ahead with something like below,

@RequestMapping(value = "user/{userid}", method = RequestMethod.DELETE)

And use

@RequestMapping(value = "/save", method = RequestMethod.POST)

for creating an user. This helps in Single Responsibility Principle and more modular code. Let your method do one thing, and do it right!

Would recommend this link for using which HttpMethod for which purpose.

1 Comment

PS: I don't know this was a old question while answering. Came up on my question feed randomly. :D

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.