这是问题的延续 Spring MVC @PathVariable被截断

Spring论坛声明它已经固定(3.2版本)作为ContentNegotiationManager的一部分。请看下面的链接。 https://jira.springsource.org/browse/SPR-6164 https://jira.springsource.org/browse/SPR-7632

在我的应用程序中,带有。com的requestParameter被截断了。

谁能告诉我如何使用这个新功能?如何在xml中配置它?

注:春季论坛- #1 Spring MVC @PathVariable带点(.)会被截断


当前回答

在Spring Boot Rest Controller中,我通过以下步骤解决了这些问题:

RestController:

@GetMapping("/statusByEmail/{email:.+}/")
public String statusByEmail(@PathVariable(value = "email") String email){
  //code
}

从Rest客户端:

Get http://mywebhook.com/statusByEmail/abc.test@gmail.com/

其他回答

如果您正在使用Spring 3.2。x和<mvc:annotation-driven />,创建这个小BeanPostProcessor:

package spring;

public final class DoNotTruncateMyUrls implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof RequestMappingHandlerMapping) {
            ((RequestMappingHandlerMapping)bean).setUseSuffixPatternMatch(false);
        }
        return bean;
    }
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}

然后把这个放在你的MVC配置xml中:

<bean class="spring.DoNotTruncateMyUrls" />

除了Martin Frey的回答,这个问题也可以通过在RequestMapping值中添加一个尾随斜杠来修复:

/path/{variable}/

请记住,此修复程序不支持可维护性。现在它要求所有的URI后面都有一个斜杠——这对API用户/新开发人员来说可能不太明显。因为不是所有的参数都有。在它们中,它还可能产生间歇性的错误

解决这个问题的一个非常简单的方法是在后面添加一个斜杠…

例如:

使用:

/somepath/filename.jpg/

而不是:

/somepath/filename.jpg

/ somepath /{变量:。+}适用于Java requestMapping标签。

在spring 4.2的路径名中包含电子邮件地址的完整解决方案是

<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>
<mvc:annotation-driven
    content-negotiation-manager="contentNegotiationManager">
    <mvc:path-matching suffix-pattern="false" registered-suffixes-only="true" />
</mvc:annotation-driven>

将其添加到应用程序xml中