【问题标题】:SPring 3.2 Path variable truncatingSPring 3.2 路径变量截断
【发布时间】:2014-11-26 08:54:07
【问题描述】:

我知道以前有人问过这个问题。然而,尽管按照建议做了所有事情,但我遇到了一个问题。

如果我的路径变量具有 .jpg 扩展名,它将被截断。 这是我的设置

 <mvc:annotation-driven
        content-negotiation-manager="contentNegotiationManager">
        <mvc:message-converters>
            <bean
                class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>image/jpeg</value>
                        <value>image/jpg</value>
                        <value>image/png</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>

    </mvc:annotation-driven>

    <bean id="contentNegotiationManager"
        class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        <property name="favorPathExtension" value="false" />
    </bean>

<bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <util:list>
                <ref bean="jsonMessageConverter" />
            </util:list>
        </property>
    </bean>

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

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

控制器

@RequestMapping(value = "image/{fileName}", method = RequestMethod.GET, produces = { MediaType.IMAGE_JPEG_VALUE,
            MediaType.IMAGE_GIF_VALUE, MediaType.IMAGE_PNG_VALUE })
    public @ResponseBody
    byte[] getImage(@PathVariable("fileName") final String fileName);

我还需要做更多吗?

【问题讨论】:

  • 您是否真的尝试了stackoverflow.com/questions/16332092/… 下的所有建议。我认为最好列出所有你尝试过的,否则你会得到很多你已经尝试过的答案
  • 查看this了解正确的语法
  • 我尝试过使用内容协商管理器。我将喜爱路径扩展设置为假,但由于某种原因它不起作用。我正在使用 Spring 3.2.6
  • @Yogi 你能发布一个有这个问题的控制器吗?
  • @JavaBond 我已经发布了。谢谢

标签: spring spring-mvc path-variables


【解决方案1】:

试试这个(注意更新后的value@RequestMapping

@RequestMapping(value = "image/{fileName:[a-z]\\.[a-z]}}", method = RequestMethod.GET, produces = { MediaType.IMAGE_JPEG_VALUE,
        MediaType.IMAGE_GIF_VALUE, MediaType.IMAGE_PNG_VALUE })
public @ResponseBody
byte[] getImage(@PathVariable("fileName") final String fileName);

参见参考here

【讨论】:

  • 我也可以使用 :.+ 作为正则表达式。但是我觉得这不应该是必要的
  • 分享你的结果/发现
猜你喜欢
  • 2015-02-09
  • 1970-01-01
  • 1970-01-01
  • 2017-01-03
  • 2015-03-15
  • 2014-08-03
  • 2013-11-20
  • 2017-01-11
相关资源
最近更新 更多