Mapping the same url to different methods based on request body in spring

franksands

I want to know if it's possible to map the same URL to different methods in the RestController class, based only in the request body. For example:

@RequestMapping(value="/delete", method=RequestMethod.POST )
public void delete(@RequestBody String id) {
    //do something
}

@RequestMapping(value="/delete", method=RequestMethod.POST )
public void delete(@RequestBody Book book) {
    //do something
}

The request body will always be a JSON payload. if it's {"id":"foo"} I want the first method to be called. If the request body is:

{
    "title":"Spring Guide",
    "author":"John Doe"
}

I want the second method to be called. Is this possible?

franksands

There is no way to differentiate only by payload.

Based on the tests I did here and M. Deinum and Ali Dehghani's response I think the best way to do this is to have different urls for each case. So a /books/{id} to delete by the id, and a /books with the object's JSON in the body to delete passing the object. Thanks for all that commented.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can two rest resources have same methods and request mapping but different @path(url) in Restful service

From Dev

Spring MVC is confusing get and post methods with the same request mapping

From Dev

Mapping more than one url to same controller but different methods

From Dev

Request Mapping in Spring MVC. Identical/Same function, just different mapping, fails

From Dev

Handle same URL in different controllers based on parameters - Spring MVC

From Dev

Spring Request Mapping based on any/no query string

From Dev

Spring - same mapping to display different list

From Dev

Render different static folder based on url request

From Dev

How to map different HTTP methods on the same url to different controllers?

From Dev

Spring MVC Based Rest Services Validations for request body

From Dev

Spring + Tomcat multi tenancy based on Request URL

From Dev

Spring + Tomcat multi tenancy based on Request URL

From Dev

How to change spring request mapping to disallow url pattern with suffix

From Dev

spring http request mapping single uri with different request parameters to different method is good practice or bad practice

From Dev

Spring REST request mapping

From Dev

Request Mapping on Spring by Javascript

From Dev

Multiple URL mapping with same filters spring security 3.x

From Dev

Which status to return for request to invalid URL for different http methods?

From Dev

Java : different init parameters to same servlet based on different URL patteren

From Dev

Different URL based on same server but different DNS entry

From Dev

Spring Data: Deleting entites at the same time using different methods?

From Dev

Mapping URL in Spring MVC

From Dev

How to send Ajax request to different url of same server

From Dev

How to send Ajax request to different url of same server

From Dev

Spring MVC Controller working but not creating the specified response URL ,It is creating the url from request mapping string

From Dev

Spring request mapping wildcard exceptions

From Dev

Spring MVC request mapping not working

From Dev

Spring boot Request Mapping with Schedule

From Dev

Spring request mapping wildcard exceptions

Related Related

  1. 1

    Can two rest resources have same methods and request mapping but different @path(url) in Restful service

  2. 2

    Spring MVC is confusing get and post methods with the same request mapping

  3. 3

    Mapping more than one url to same controller but different methods

  4. 4

    Request Mapping in Spring MVC. Identical/Same function, just different mapping, fails

  5. 5

    Handle same URL in different controllers based on parameters - Spring MVC

  6. 6

    Spring Request Mapping based on any/no query string

  7. 7

    Spring - same mapping to display different list

  8. 8

    Render different static folder based on url request

  9. 9

    How to map different HTTP methods on the same url to different controllers?

  10. 10

    Spring MVC Based Rest Services Validations for request body

  11. 11

    Spring + Tomcat multi tenancy based on Request URL

  12. 12

    Spring + Tomcat multi tenancy based on Request URL

  13. 13

    How to change spring request mapping to disallow url pattern with suffix

  14. 14

    spring http request mapping single uri with different request parameters to different method is good practice or bad practice

  15. 15

    Spring REST request mapping

  16. 16

    Request Mapping on Spring by Javascript

  17. 17

    Multiple URL mapping with same filters spring security 3.x

  18. 18

    Which status to return for request to invalid URL for different http methods?

  19. 19

    Java : different init parameters to same servlet based on different URL patteren

  20. 20

    Different URL based on same server but different DNS entry

  21. 21

    Spring Data: Deleting entites at the same time using different methods?

  22. 22

    Mapping URL in Spring MVC

  23. 23

    How to send Ajax request to different url of same server

  24. 24

    How to send Ajax request to different url of same server

  25. 25

    Spring MVC Controller working but not creating the specified response URL ,It is creating the url from request mapping string

  26. 26

    Spring request mapping wildcard exceptions

  27. 27

    Spring MVC request mapping not working

  28. 28

    Spring boot Request Mapping with Schedule

  29. 29

    Spring request mapping wildcard exceptions

HotTag

Archive