这是问题的延续 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中,正则表达式解决了这样的问题

@GetMapping("/path/{param1:.+}")

其他回答

如果您正在使用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" />

如果您正在使用Spring 3.2+,那么下面的解决方案将有所帮助。这将处理所有的url,所以肯定比在请求URI映射中应用regex模式更好。喜欢/ somepath /{变量:。+}

在xml文件中定义一个bean

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

标志的用法可以在文档中找到。我把剪报解释一下

解释useRegisteredSuffixPatternMatch据说可以解决这个问题。来自类中的java文档

If enabled, a controller method mapped to "/users" also matches to "/users.json" assuming ".json" is a file extension registered with the provided {@link #setContentNegotiationManager(ContentNegotiationManager) contentNegotiationManager}. This can be useful for allowing only specific URL extensions to be used as well as in cases where a "." in the URL path can lead to ambiguous interpretation of path variable content, (e.g. given "/users/{user}" and incoming URLs such as "/users/john.j.joe" and "/users/john.j.joe.json").

下面是一种完全依赖java配置的方法:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@Configuration
public class MvcConfig extends WebMvcConfigurationSupport{

    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        RequestMappingHandlerMapping handlerMapping = super.requestMappingHandlerMapping();
        handlerMapping.setUseSuffixPatternMatch(false);
        handlerMapping.setUseTrailingSlashMatch(false);
        return handlerMapping;
    }
}

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

/path/{variable}/

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

简单的解决方案:添加一个正则表达式{q:。+}在@RequestMapping

@RequestMapping("medici/james/Site")
public class WebSiteController {

    @RequestMapping(value = "/{site:.+}", method = RequestMethod.GET)
    public ModelAndView display(@PathVariable("site") String site) {
        return getModelAndView(site, "web site");

    }
}

现在,对于input /site/jamesmedice.com,“site”将显示正确的james's site