0

I would like to know which one of the above are better/correct/most_used or what. The first, using value in the @RequestMapping or the other using path.

    @RequestMapping(value = { "/isbn/{isbnCode}" }, method = RequestMethod.GET)
    public ResponseEntity<?> findByIsbnCode(@PathVariable String isbnCode) {
        Book obj = service.findByIsbnCode(isbnCode);

        return ResponseEntity.ok().body(obj);
    }
// Request: http://localhost:8080/books/title?title=book_title

    @RequestMapping(method = RequestMethod.GET, path = { "/title" })
    public ResponseEntity<?> findByTitle(@RequestParam(value = "title") String title) {
        Book obj = service.findByTitle(title);

        return ResponseEntity.ok().body(obj);
    }
// Request: http://localhost:8080/books/isbn/978-84-663-4612-2

Both works. Just wanna find out the differences between the two.

Thanks in advance!

balias
  • 499
  • 1
  • 4
  • 17
romeucr
  • 169
  • 1
  • 13
  • 1
    Possible Duplicate Question: https://stackoverflow.com/questions/50351590/difference-between-path-and-value-attributes-in-requestmapping-annotation – Bilal Kamran Sep 21 '20 at 14:03

2 Answers2

1

"URI parameter (Path Param/variable denoted by @PathVariable) is basically used to identify a specific resource or resources whereas Query Parameter(or request param denoted by @RequestParam) is used to sort/filter those resources." The first example makes use of path variable, here you are extracting value from the url itself, whereas in second example you are fetching a request/query parameter. References: https://dzone.com/articles/understanding-the-uri-param-and-query-param-with-r https://blog.restcase.com/7-rules-for-rest-api-uri-design/#:~:text=URIs%20should%20follow%20a%20predictable,APIs%20are%20written%20for%20consumers.

balias
  • 499
  • 1
  • 4
  • 17
1

JPA is the Java Persistence API, and it has nothing to do with @RequestMapping;

You are asking about the difference(s) between path and value of the @RequestMapping:

Just wanna find out the differences between the two.

path and value elements are aliases for each other, and there are only minor differences I can see from the Spring Documentation:

When using path:

  • Ant-style path patterns are also supported (e.g. "/profile/**");
  • Placeholders (e.g. "/${profile_path}") can be used.
Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66