带有点(。)的Spring MVC @PathVariable被截断

本文翻译自:Spring MVC @PathVariable with dot (.) is getting truncated

This is continuation of question Spring MVC @PathVariable getting truncated 这是问题Spring MVC @PathVariable被截断的延续

Spring forum states that it has fixed(3.2 version) as part of ContentNegotiationManager. Spring论坛指出,它已作为ContentNegotiationManager的一部分进行了修复(3.2版)。 see the below link. 请参阅下面的链接。
https://jira.springsource.org/browse/SPR-6164 https://jira.springsource.org/browse/SPR-6164
https://jira.springsource.org/browse/SPR-7632 https://jira.springsource.org/browse/SPR-7632

In my application requestParameter with .com is truncated. 在我的应用程序中,带有.com的requestParameter被截断了。

Could anyone explain me how to use this new feature? 谁能解释我如何使用此新功能? how is it configurable at xml? 如何在xml上进行配置?

Note: spring forum- #1 Spring MVC @PathVariable with dot (.) is getting truncated 注意: Spring论坛-#1 Spring MVC @PathVariable带有点(。)


#1楼

参考:https://stackoom.com/question/16Wiq/带有点-的Spring-MVC-PathVariable被截断


#2楼

As far as i know this issue appears only for the pathvariable at the end of the requestmapping. 据我所知,这个问题只出现在requestmapping末尾的pathvariable中。

We were able to solve that by defining the regex addon in the requestmapping. 我们可以通过在requestmapping中定义regex插件来解决这一问题。

 /somepath/{variable:.+}

#3楼

If you are using Spring 3.2.x and <mvc:annotation-driven /> , create this little BeanPostProcessor : 如果您使用的是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;
    }
}

Then put this in your MVC config xml: 然后将其放在您的MVC配置xml中:

<bean class="spring.DoNotTruncateMyUrls" />

#4楼

In addition to Martin Frey's answer, this can also be fixed by adding a trailing slash in the RequestMapping value: 除了Martin Frey的答案外,还可以通过在RequestMapping值中添加尾斜杠来解决此问题:

/path/{variable}/

Keep in mind that this fix does not support maintainability. 请记住,此修复程序不支持可维护性。 It now requires all URI's to have a trailing slash - something that may not be apparent to API users / new developers. 现在,它要求所有URI都带有斜线-对于API用户/新开发人员而言,这可能并不明显。 Because it's likely not all parameters may have a . 因为可能并非所有参数都可能有一个. in them, it may also create intermittent bugs 在其中,它也可能会造成间歇性错误


#5楼

Spring considers that anything behind the last dot is a file extension such as .json or .xml and trucate it to retrieve your parameter. 春认为,最后一个点后面的东西是作为文件扩展名, .json.xml和trucate它来检索您的参数。

So if you have /somepath/{variable} : 因此,如果您使用/somepath/{variable}

  • /somepath/param , /somepath/param.json , /somepath/param.xml or /somepath/param.anything will result in a param with value param /somepath/param/somepath/param.json/somepath/param.xml/somepath/param.anything将导致具有值的PARAM param
  • /somepath/param.value.json , /somepath/param.value.xml or /somepath/param.value.anything will result in a param with value param.value /somepath/param.value.json/somepath/param.value.xml/somepath/param.value.anything将导致具有值的PARAM param.value

if you change your mapping to /somepath/{variable:.+} as suggested, any dot, including the last one will be consider as part of your parameter : 如果按照建议将映射更改为/somepath/{variable:.+} ,则任何点(包括最后一个点)都将被视为参数的一部分:

  • /somepath/param will result in a param with value param /somepath/param将导致参数值为param
  • /somepath/param.json will result in a param with value param.json /somepath/param.json将导致一个参数值为param.json的参数
  • /somepath/param.xml will result in a param with value param.xml /somepath/param.xml将导致参数值为param.xml的参数
  • /somepath/param.anything will result in a param with value param.anything /somepath/param.anything会导致参数值为param.anything
  • /somepath/param.value.json will result in a param with value param.value.json /somepath/param.value.json将导致一个参数值为param.value.json的参数
  • ... ...

If you don't care of extension recognition, you can disable it by overriding mvc:annotation-driven automagic : 如果您不关心扩展识别,则可以通过重写mvc:annotation-driven automagic来禁用它:

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

So, again, if you have /somepath/{variable} : 所以,再次,如果您有/somepath/{variable}

  • /somepath/param , /somepath/param.json , /somepath/param.xml or /somepath/param.anything will result in a param with value param /somepath/param/somepath/param.json/somepath/param.xml/somepath/param.anything将导致具有值的PARAM param
  • /somepath/param.value.json , /somepath/param.value.xml or /somepath/param.value.anything will result in a param with value param.value /somepath/param.value.json/somepath/param.value.xml/somepath/param.value.anything将导致具有值的PARAM param.value

note : the difference from the default config is visible only if you have a mapping like somepath/something.{variable} . 注意:与默认配置的区别仅在您具有诸如somepath/something.{variable}类的映射时可见。 see Resthub project issue 请参阅Resthub项目问题

if you want to keep extension management, since Spring 3.2 you can also set the useRegisteredSuffixPatternMatch property of RequestMappingHandlerMapping bean in order to keep suffixPattern recognition activated but limited to registered extension. 如果要保持扩展管理,从Spring 3.2开始,还可以设置RequestMappingHandlerMapping bean的useRegisteredSuffixPatternMatch属性,以保持激活suffixPattern识别,但仅限于已注册的扩展。

Here you define only json and xml extensions : 在这里,您仅定义json和xml扩展名:

<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>

Note that mvc:annotation-driven accepts now a contentNegotiation option to provide a custom bean but the property of RequestMappingHandlerMapping has to be changed to true (default false) (cf. https://jira.springsource.org/browse/SPR-7632 ). 请注意,mvc:annotation-driven现在接受contentNegotiation选项以提供自定义bean,但必须将RequestMappingHandlerMapping的属性更改为true(默认为false)(参见https://jira.springsource.org/browse/SPR-7632 )。

For that reason, you still have to override the all mvc:annotation-driven configuration. 因此,您仍然必须覆盖所有mvc:annotation驱动的配置。 I opened a ticket to Spring to ask for a custom RequestMappingHandlerMapping : https://jira.springsource.org/browse/SPR-11253 . 我开了一张去Spring的票,要求自定义RequestMappingHandlerMapping: https : //jira.springsource.org/browse/SPR-11253 Please vote if you are intereted in. 如果您感兴趣,请投票。

While overriding, be carreful to consider also custom Execution management overriding. 覆盖时,请谨慎考虑自定义执行管理覆盖。 Otherwise, all your custom Exception mappings will fail. 否则,所有自定义Exception映射将失败。 You will have to reuse messageCoverters with a list bean : 您将必须使用list bean重用messageCoverters:

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />

<util:list id="messageConverters">
    <bean class="your.custom.message.converter.IfAny"></bean>
    <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>
</util:list>

<bean name="exceptionHandlerExceptionResolver"
      class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver">
    <property name="order" value="0"/>
    <property name="messageConverters" ref="messageConverters"/>
</bean>

<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 name="validator" ref="validator" />
        </bean>
    </property>
    <property name="messageConverters" ref="messageConverters"/>
</bean>

<bean id="handlerMapping"
      class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
</bean>

I implemented, in the open source project Resthub that I am part of, a set of tests on these subjects : see https://github.com/resthub/resthub-spring-stack/pull/219/files & https://github.com/resthub/resthub-spring-stack/issues/217 我在我参与的开源项目Resthub中实现了针对这些主题的一系列测试:请参阅https://github.com/resthub/resthub-spring-stack/pull/219/fileshttps:// github.com/resthub/resthub-spring-stack/issues/217


#6楼

Update for Spring 4: since 4.0.1 you can use PathMatchConfigurer (via your WebMvcConfigurer ), eg Spring 4的更新:从4.0.1开始,您可以使用PathMatchConfigurer (通过WebMvcConfigurer ),例如

@Configuration
protected static class AllResources extends WebMvcConfigurerAdapter {

    @Override
    public void configurePathMatch(PathMatchConfigurer matcher) {
        matcher.setUseRegisteredSuffixPatternMatch(true);
    }

}

In xml, it would be ( https://jira.spring.io/browse/SPR-10163 ): 在xml中,它将是( https://jira.spring.io/browse/SPR-10163 ):

<mvc:annotation-driven>
    [...]
    <mvc:path-matching registered-suffixes-only="true"/>
</mvc:annotation-driven>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值