Bind Path variables to a custom model object in spring

我们两清 提交于 2019-12-17 09:46:15

问题


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?


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/17149425/bind-path-variables-to-a-custom-model-object-in-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!