Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind Path variables to a custom model object in spring

I have a class that models my request, something like

class Venue {
    private String city;
    private String place;

    // Respective getters and setters.
}

And I want to support a RESTful URL to get information about a venue. So I have controller method like this.

@RequestMapping(value = "/venue/{city}/{place}", method = "GET")
public String getVenueDetails(@PathVariable("city") String city, @PathVariable("place") String place, Model model) {
    // code
}

Is there a way, I can say in spring to bind my path variables to the model object (in this case Venue) instead of getting every individual parameter?

like image 428
Senthil Babu Avatar asked Jun 17 '13 13:06

Senthil Babu


3 Answers

Spring MVC offers the ability to bind request params and path variables into a JavaBean, which in your case is Venue. For example:

@RequestMapping(value = "/venue/{city}/{place}", method = "GET")
public String getVenueDetails(Venue venue, Model model) {
    // venue object will be automatically populated with city and place
}

Note that your JavaBean has to have city and place properties for it to work.

For more information, you can take a look at the withParamGroup() example from spring-projects/spring-mvc-showcase

like image 109
Liang Zhou Avatar answered Sep 30 '22 04:09

Liang Zhou


As per the Spring documentation available at http://static.springsource.org/spring/docs/3.2.3.RELEASE/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates, automatic support is provided to simple types only:

A @PathVariable argument can be of any simple type such as int, long, Date, etc. Spring automatically converts to the appropriate type or throws a TypeMismatchException if it fails to do so.

I haven't tried this specific combination of @RequestParam and Model, but it looks like you can achieve your desired implementation by creating a custom WebBindingInitializer, as detailed at http://static.springsource.org/spring/docs/3.2.3.RELEASE/spring-framework-reference/html/mvc.html#mvc-ann-typeconversion.

The custom class will have access to the WebRequest and would return a domain object populated with data extracted from this request.

like image 44
vincentks Avatar answered Sep 30 '22 03:09

vincentks


You could provide an implementation of the HandlerMethodArgumentResolver interface. This interface is mainly used to resolve arguments to your controller methods in ways that Spring can't (which is essentially what you are trying to do). You accomplish this by implementing these two methods:

public boolean supportsParameter(MethodParameter mp) {
    ...
}

public Object resolveArgument(mp, mavc, nwr, wdbf) throws Exception {
    ...
}

You can inject your implementation into the Spring context via:

<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <bean class="com.path.ImplementationOfHandlerMethodArgumentResolver"/>
    </mvc:argument-resolvers>
</mvc:annotation-driven>

When Spring encounters a parameter it can't resolve, it will call your method supportsParameter() to see if your resolver can resolve the parameter. If your method returns true, then Spring will call your resolveArgument() method to actually resolve the parameter. In this method, you have access to the NativeWebRequest object, which you can use to grab the path beyond the contextPath. (in your case it would be: /venue/{city}/{place}) you can parse through the request path and attempt to get the city/place Strings to populate in your Venue Object.

like image 43
Paul Gray Avatar answered Sep 30 '22 04:09

Paul Gray