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

Yashwantrao Raut

My question is I need to implement rest api to search Students bases on its attributes below are my urls.

    http/my system/vo1/students/search?firstname=ABC&responseType=summary
    http/my system/vo1/students/search?age=ABC&responseType=summary
    http/my system/vo1/students/search?id=ABC& responseType =summary
    http/my system/vo1/students/search?mobile=ABC& responseType =summary
    http/my system/vo1/students/search?lastname=ABC& responseType =summary
    http/my system/vo1/students/search?education=ABC& responseType=summary
    http/my system/vo1/students/search?working=ABC& responseType =summary
    http/my system/vo1/students/search? responseType =summary

responseType can be summary,hierarchy or can more types will be added in feature.

So i have single URI "/my system/vo1/students/search?" with different parameters and user will send max two optional parameters and min is one is mandatory that is responseType

in spring we can map single uri with different request parameters to different method as shown below

@RequestMapping(value = “my system/vo1/students/search", method = RequestMethod.GET)
        @ResponseBody
        public Students  searchStudentByName(
                @RequestParam(value = “name",required = true) String name,
                @RequestParam(value = “ responseType", required = true) String responseType)
    {
            //do student fetching works
    }

    @RequestMapping(value = “my system/vo1/students/search", method = RequestMethod.GET)
        @ResponseBody
        public Students  searchStudentByAge(
                @RequestParam(value = “age",required = true) int age,
                @RequestParam(value = “ responseType", required = true) String responseType)
    {
            //do student fetching works
    }

and so on .. we can write 9 different methods for this .

one more way is to do it create only one method.

 @RequestMapping(value = “my system/vo1/students/search", method = RequestMethod.GET)
        @ResponseBody
        public Students  searchStudentByAge(
                @RequestParam(value = “firstname",required = true) String age,
                @RequestParam(value = “age",required = false) int age,
                @RequestParam(value = “lastname",required = false) String lastname,
                @RequestParam(value = “working",required = false) String working,
             //add more for each field that will be present in url...........
                @RequestParam(value = “ responseType", required = true) String responseType)
    {
            //do student fetching works
    }

In second approach we can write separate validator to validate the input request.

I have below Questions

  1. which approach is best (considering best design principals and practices)
  2. why it is best approach
  3. what is best object oriented design for such problem
Neil McGuigan

You should definitely go with one RequestMapping in this scenario (the 2nd option). What if you later want to combine query params? Combinatorial Explosion!

Make sure you understand the JPA Criteria API, or check out Jooq and QueryDSL. That will make life easier for you.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is it bad practice for a REST endpoint to return different response fields based on the request?

From Dev

Spring - No mapping found for HTTP request with URI

From Dev

Spring ViewResolver : no mapping found for HTTP request with URI

From Dev

Spring - No mapping found for HTTP request with URI

From Dev

Spring ViewResolver : no mapping found for HTTP request with URI

From Dev

Is it against good practice to throw twice in a single method, if both exceptions have different conditions?

From Dev

Is it a bad practice to create a single comparator for two completely different classes?

From Dev

Spring: No mapping found for HTTP request with URI [uri] in Dispatcher Servlet

From Dev

PHP Classes load() method good/bad practice?

From Dev

No mapping found for HTTP request with URI

From Dev

In general, is it good practice to fail a REST request if the request body includes extra parameters?

From Dev

Inheritance best practice to share code when method parameters are different?

From Dev

Spring MVC. No mapping found for HTTP request with URI

From Dev

Spring configuration: No mapping found for HTTP request with URI in DispatcherServlet with name 'dispatcherServlet'

From Dev

Java Spring MVC: No mapping found for HTTP request with URI

From Dev

No mapping found for HTTP request with URI (spring 4.1 annotation configuration)

From Dev

Spring no mapping found for HTTP request with URI[require.js] in DispatcherServlet

From Dev

Deployment failing in Spring MVC and tiles with No mapping found for HTTP request with URI

From Dev

No mapping found for HTTP request with URI with Spring Data Rest

From Dev

Spring MVC. No mapping found for HTTP request with URI

From Dev

No mapping found for HTTP request with URI Spring MVC 4

From Dev

Is it a good practice to reload credentials before every request?

From Dev

Is it a bad practice to do search using post request?

From Dev

More [GET] request-response examples with different URI parameters

From Dev

Spring MVC bean mapping to HTTP GET request parameters similar to @BeanParam

From Dev

Spring RestTemplate HTTP Post with parameters cause 400 bad request error

From Dev

Are tables with the same attributes but different relationships bad practice?

From Dev

Is it a bad practice to append operations to a Stream in different methods?

From Dev

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

Related Related

  1. 1

    Is it bad practice for a REST endpoint to return different response fields based on the request?

  2. 2

    Spring - No mapping found for HTTP request with URI

  3. 3

    Spring ViewResolver : no mapping found for HTTP request with URI

  4. 4

    Spring - No mapping found for HTTP request with URI

  5. 5

    Spring ViewResolver : no mapping found for HTTP request with URI

  6. 6

    Is it against good practice to throw twice in a single method, if both exceptions have different conditions?

  7. 7

    Is it a bad practice to create a single comparator for two completely different classes?

  8. 8

    Spring: No mapping found for HTTP request with URI [uri] in Dispatcher Servlet

  9. 9

    PHP Classes load() method good/bad practice?

  10. 10

    No mapping found for HTTP request with URI

  11. 11

    In general, is it good practice to fail a REST request if the request body includes extra parameters?

  12. 12

    Inheritance best practice to share code when method parameters are different?

  13. 13

    Spring MVC. No mapping found for HTTP request with URI

  14. 14

    Spring configuration: No mapping found for HTTP request with URI in DispatcherServlet with name 'dispatcherServlet'

  15. 15

    Java Spring MVC: No mapping found for HTTP request with URI

  16. 16

    No mapping found for HTTP request with URI (spring 4.1 annotation configuration)

  17. 17

    Spring no mapping found for HTTP request with URI[require.js] in DispatcherServlet

  18. 18

    Deployment failing in Spring MVC and tiles with No mapping found for HTTP request with URI

  19. 19

    No mapping found for HTTP request with URI with Spring Data Rest

  20. 20

    Spring MVC. No mapping found for HTTP request with URI

  21. 21

    No mapping found for HTTP request with URI Spring MVC 4

  22. 22

    Is it a good practice to reload credentials before every request?

  23. 23

    Is it a bad practice to do search using post request?

  24. 24

    More [GET] request-response examples with different URI parameters

  25. 25

    Spring MVC bean mapping to HTTP GET request parameters similar to @BeanParam

  26. 26

    Spring RestTemplate HTTP Post with parameters cause 400 bad request error

  27. 27

    Are tables with the same attributes but different relationships bad practice?

  28. 28

    Is it a bad practice to append operations to a Stream in different methods?

  29. 29

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

HotTag

Archive