0

I' m developing a REST WebApp with spring framework, now i'd like to know the best practice to receive data. It s best to retrieve them with @RequestParam or @PathVariable?Obviously i'm interested in receiving simple data like an userid or an username. Using an example:

@RequestMapping(value = "/getdata", method = RequestMethod.GET)
public String GetData(@RequestParam(value = "memberId", required = true) Integer memberId){
  //return data for user with Id = memberid;
}

or

@RequestMapping(value = "/getdata/{memberid}", method = RequestMethod.GET)
public String GetData(@pathvariable int memberid){
  //return data for user with Id = memberid;
}

I'm interested about REST web services

Raedwald
  • 40,290
  • 35
  • 127
  • 207
Davide Quaglio
  • 651
  • 1
  • 10
  • 26
  • 1
    http://stackoverflow.com/a/4028874/2504224, http://stackoverflow.com/questions/3198492/rest-standard-path-parameters-or-request-parameters – geoand Jun 25 '14 at 13:47

4 Answers4

1

Rest convention is to use uri variables :

get http://stackoverflow.com/users/3630157
NimChimpsky
  • 43,542
  • 55
  • 186
  • 295
0

@PathVarible is used in RESTful web service. Learn what is REST . And the method name should start with lower case.

Yang Lifan
  • 265
  • 4
  • 14
0

It depends on what kind of data you want to receive in the Controller.

You can use @PathVariable to get information from the uri.

Url:

exampleUrl/Application/example/someData

Controller:

@RequestMapping(value="example/{someData}", method = RequestMethod.GET)
public String Controller(@PathVariable("someData") String someData){        
  ...
}

And you can use @RequestParam to get a parameter from the url.

Url:

exampleUrl/Application/example/someData?parameter1=hello

Controller:

@RequestMapping(value = "example/someData", method = RequestMethod.GET)
public String Controller(@RequestParam(value = "parameter1", required = false) String parameter1){        
          ...
        }

So there is no "better" way, it depends on how you want to handle your request and what kind of information you need.

George Ant
  • 371
  • 1
  • 6
  • But if i the information is the same, like in the example..I thought there was a convention or a best-practice to follow... – Davide Quaglio Jun 25 '14 at 14:23
0

Create a model map and add those parameter name/value pairs to it:

@RequestMapping(value="/student/{username}/", method=RequestMethod.GET)

public String adminStudent(@PathVariable String username, @RequestParam String studentid, Model model)
{
    model.put("username", username);
    model.put("studentid", studentid);
    return "student";
}
Steve
  • 8,822
  • 10
  • 42
  • 73