How do I restrict route extensions in @RequestMapp

2019-02-19 13:58发布

问题:

I have a fairly simple task that I want to accomplish, but can't seem to find information for Spring MVC routing about it. I have a very simple controller that routes a path to a view:

@Controller
@RequestMapping(value = "/help")
public class HelpController {

    private static final String HELP = "help";

    @RequestMapping(method = RequestMethod.GET)
    public String help(Model model, Locale locale) {
        model.addAttribute("locale", locale);
        return HELP;
    }
}

I would like to throw a 404 if http://mysite.com/help.some.extension.is.entered, but Spring seems to resolve the example to /help. The javadoc says that the @RequestMapping annotation is just a servlet URI mapping, but I thought /help means it needs to be an exact match. Any clarification would be appreciated.

回答1:

For Spring 4 it's pretty easy to solve:

<mvc:annotation-driven>
    <mvc:path-matching suffix-pattern="false" />
</mvc:annotation-driven>

So you still can use mvc:annotation-driven for your config.



回答2:

You can mention it in the @RequestMapping annotation

it is same as Servlet URL pattern only.

@Controller
public class HelpController {

    private static final String HELP = "help";

    @RequestMapping(value = "/help" method = RequestMethod.GET)
    public String help(Model model, Locale locale) {
        model.addAttribute("locale", locale);
        return HELP;
    }

    @RequestMapping(value = "help/*" method = RequestMethod.GET)
    public String helpWithExtraWords() {
        return "error";
    }
}


回答3:

The best way I can think of is to configure your RequestMappingHandlerMapping explicitly to not consider suffixpaths, this way:

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

However, if you have configured your Spring MVC using mvc:annotation-driven, this will not work, you will have to expand out the entire handlerAdapter definition, which is not that difficult to do, along these lines(this is not complete, you can look through org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser for the entire definition):

<bean name="handlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="webBindingInitializer">
        <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
            <property name="conversionService" ref="conversionService"></property>
            <property name="validator">
                <bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
            </property>
        </bean>
    </property>
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
        </list>
    </property>
</bean>

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


回答4:

With Spring 3.0.X You can use the useDefaultSuffixPattern property.

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="useDefaultSuffixPattern" value="false" />
</bean>

You will need to remove </mvc:annotation-driven>

Refer URL Pattern Restricting in SPRING MVC