Bind Path variables to a custom model object in spring

前端 未结 3 1722
無奈伤痛
無奈伤痛 2020-11-30 07:39

I have a class that models my request, something like

class Venue {
    private String city;
    private String place;

    // Respective getters and setters         


        
3条回答
  •  春和景丽
    2020-11-30 07:52

    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.

提交回复
热议问题