我有一个相当简单的任务,我想完成,但似乎无法找到有关它的Spring MVC路由信息。我有一个非常简单的控制器,它将路径路由到视图:
@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;
}
}
如果 http://mysite.com/help.some.extension.is.entered ,我想抛出404,但Spring似乎将示例解析为/ help。 javadoc说@RequestMapping注释只是一个servlet URI映射,但我认为/ help意味着它需要完全匹配。任何澄清将不胜感激。
答案 0 :(得分:1)
对于Spring 4,它很容易解决:
<mvc:annotation-driven>
<mvc:path-matching suffix-pattern="false" />
</mvc:annotation-driven>
因此,您仍然可以使用mvc:annotation-driven
作为配置。
答案 1 :(得分:0)
您可以在@RequestMapping注释
中提及它它仅与Servlet URL模式相同。
@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";
}
}
答案 2 :(得分:0)
我能想到的最好的方法是将RequestMappingHandlerMapping显式配置为不考虑后缀路径,这样:
<bean name="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="useSuffixPatternMatch" value="false"></property>
</bean>
但是,如果你使用mvc:annotation-driven
配置了你的Spring MVC,这将不起作用,你将不得不扩展整个handlerAdapter定义,这并不难做到这一点(这不是完成后,您可以通过org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser
查看整个定义:
<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>
答案 3 :(得分:0)
使用Spring 3.0.X您可以使用useDefaultSuffixPattern属性。
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="useDefaultSuffixPattern" value="false" />
</bean>
您需要删除</mvc:annotation-driven>