resthub / resthub-spring-stack

RESThub Spring stack
http://resthub.org/spring-stack.html
Other
121 stars 66 forks source link

Alternative to mvc:annotation-driven and params with dots improvements #217

Closed bmeurant closed 10 years ago

bmeurant commented 10 years ago

Alternative to mvc:annotation-driven

Last versions of Spring MVC provide a new way to override default behaviour related to request params with dots and extensions management.

The new configuration strongly depends on concrete project needs and a unique config cannot allow all possibilities.

Overriding this config necessitates to redefine ContentNegotiationManager and/or RequestMappingHandlerMapping that are defaultly defined by mvc:annotation-driven.

Since spring 3.2, mvc:annotation-driven allows to provide a custom ContentNegotiationManager but in many cases, RequestMappingHandlerMapping should also be defined to set a property and this cannot be done with mvc:annotation-driven.

For these reasons and in a general point of view, we need to provide a better way to end users to customize these behaviours and mvc:annotation-driven does not allow this : we need to provide a complete configuration, miror of mvc-annotation-driven behaviour but easier to extend without redefining resthub key features such as Exception mapping.

see :

With such customization, users will be able to finely manage request params with dots :

Consider a controller mapped on "/dot" with these two methods :

@RequestMapping(method = RequestMethod.GET, value = "{param}")
    public @ResponseBody String getTest(@PathVariable String param) {
        return param;
    }

    @RequestMapping(method = RequestMethod.GET, value = "dot.{param}")
    public @ResponseBody String getDotTest(@PathVariable String param) {
        return param;
    }

The behaviour will be really different depending on the configuration :

Default behaviour (no conf needed)

conf : no

behaviour :

conf : useSuffixPatternMatch=false

<bean id="handlerMapping"
          class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
        <property name="useSuffixPatternMatch" value="false"/>
    </bean>

behaviour :

conf : useRegisteredSuffixPatternMatch=true

<bean id="handlerMapping"
          class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
        <property name="contentNegotiationManager" ref="contentNegotiationManager"/>
        <property name="useRegisteredSuffixPatternMatch" value="true"/>
    </bean>

    <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        <property name="favorPathExtension" value="false"/>
        <property name="favorParameter" value="true"/>
        <property name="mediaTypes">
            <value>
                json=application/json
                xml=application/xml
            </value>
        </property>
    </bean>

behaviour :

An alternative to mvc:annotation-driven will allow to implements the last two behaviours without redefining all resthub conf (not even explicitely defined today because of mvc automagic)

this issue is related to #188

lhmoraes commented 10 years ago

That is amazing and something that I'm looking for my project. In my project, I need to know if in the request coming ".json", ".xml" and etc. So, what is the code to get it ?

Thanks