Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 448 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Spring Web MVC: Use same request mapping for request parameter and path variable

#1
Is there a way to express that my Spring Web MVC controller method should be matched either by a request handing in a ID as part of the URI path ...

@RequestMapping(method=RequestMethod.GET, value="campaigns/{id}")
public String getCampaignDetails(Model model, @PathVariable("id") Long id) {

... or if the client sends in the ID as a HTTP request parameter in the style ...

@RequestMapping(method=RequestMethod.GET, value="campaigns")
public String getCampaignDetails(Model model, @RequestParam("id") Long id) {

This seems to me a quite common real-world URL scheme where I don't want to add duplicate code, but I wasn't able to find an answer yet. Any advice highly welcome.

**EDIT:** It turns out that there seems currently *(with Spring MVC <= 3.0)* no way to achieve this, see discussion inside Javi's answer.
Reply

#2
You can set both mapping url for the same function and setting id as optional.

@RequestMapping(method=RequestMethod.GET, value={"/campaigns","/campaigns/{id}"})
public String getCampaignDetails(Model model,
@RequestParam(value="id", required=false) Long id,
@PathVariable("id") Long id2)
{
}

though it would map as well when id is not sent, but you can control this inside the method.

**EDIT**: The previous solution doesn't work because `@PathVariable` is not set to `null` when there isn't `{null}` and it cannot map the URL (thanks ngeek). I think then that the only possible solution is to create two methods each one mapped with its `@MappingRequest` and inside one of them call the other function or redirect to the other URL (redirect: or forward: Spring prefixes). I know this solution is not what you're looking for but think it's the best you can do. Indeed you're not duplicating code but you're creating another function to handle another URL.
Reply

#3
If you still want to stick to PathVariable approach and if you are getting 400 syntactically incorrect error then follow this approach-




@RequestMapping(method=RequestMethod.GET, value={"campaigns/{id}","campaigns"})
public String getCampaignDetails(Model model,
@PathVariable Map<String, String> pathVariables)
{

System.out.println(pathVariables.get("id"));

}
Reply

#4
The `@RequestMapping` annotation now supports setting the `path` attribute instead of `name` or `value`. With `path`, you can achieve the mapping desired by this question:

<!-- language: java -->

@RequestMapping(method=RequestMethod.GET, path="campaigns/{id}")
public String getCampaignDetails(Model model, @PathVariable("id") Long id) {

@RequestMapping(method=RequestMethod.GET, value="campaigns")
public String getCampaignDetails(Model model, @RequestParam("id") Long id) {

Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through