Reputation: 151
I am trying to map a url with a Spring Controller. I want the variable petitionId at the end of my url, for instance, I want to map:
.../product/all/petitionId/{petitionId}
as well as
.../product/productId/{productId}/clientId/{clientId}/petitionId/{petitionId}
To do so, I have tried to have a RequestMapping in the controller header, like this
@Controller
@RequestMapping(value = "product/*/petitionId/{petitionId}")
public class ProductController
and in the declaration of the method I want to map
@RequestMapping(value = "*/all/*", method = RequestMethod.GET)
public @ResponseBody
String getProducts(@PathVariable Long petitionId)
I have also tried with and without slashes with one, two and no asterisks... with same 404 Error result. The request I want to make is
http://192.168.1.27:9999/middleware/product/all/petitionId/20
I know I may have the full URL mapping in each method, yet that's not the most elegant way to do it. Does anybody know how to solve this?
Upvotes: 0
Views: 1866
Reputation: 1789
Use the annotation @RequestMapping
by function.
You can use it in the class but only to write less in each function's requestMapping. Put in the class just what you have in common in all the functions of your controller.
For example:
@Controller
@RequestMapping(value = "/products")
public class ProductController {
...
@RequestMapping(value = "", method = RequestMethod.GET)
public @ResponseBody
String getProducts() { ... }
@RequestMapping(value = "/{productId}", method = RequestMethod.GET)
public @ResponseBody
String getProductsById(@PathVariable Long productId) { ... }
@RequestMapping(value = "/{productId}/clients/{clientId}/petitions/{petitionId}", method = RequestMethod.GET)
public @ResponseBody
String getPetition(@PathVariable Long productId, @PathVariable Long clientId, @PathVariable Long petitionId) { ... }
}
You'll end up with the following mappings:
/products
/products/{productId}
/products/{productId}/clients/{clientId}/petitions/{petitionId}
Upvotes: 1
Reputation: 4663
To be honest, your URLs look somewhat complicated.
Have you considered other URL schemes, e.g. for all products by petition:
GET http://192.168.1.27:9999/middleware/petitions/20/products
or product by id, client id and petition id:
GET http://192.168.1.27:9999/middleware/products?clientId=10&productId=10&petitionId=20
?
Upvotes: 0