I am trying to develop a REST service with Spring 3.1 where users can access information. A resource can easily be accessed by app/courses/1 to retrieve a course by it’s id.
However, I want to make it possible to search. Here is the model:
public class Course {
private Long id;
private String name;
private Long points;
private Long numberOfParticipants;
}
For example, what if I wanted to fetch a Course that has name=foo and points=1337, the corresponding query string would be as follows: app/courses?name=foo&points=1337.
I have come up with a temporary solution:
@RequestMapping(value = "courses")
@ResponseBody
public Course getCourse(@RequestParam(value = "name", required = false) String name,
@RequestParam(value ="points", required = false) Long points) {
// TODO
// Find and return course from the database.
}
However, this seems very tedious and messy, my question is therefore: does Spring have something that simplifies this a bit so I don’t have to harvest all the attributes the hard way?
This will map all request parameters to fields of a
Coursecommand object.