SPring 3.2 Path variable truncating

412 views Asked by At

I know this has been asked before. However In spite of doing everything as suggested I am facing an issue.

if my path variable has .jpg extension, it is getting truncated. Here are my settings

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

Controller

@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);

Do I need to do more?

1

There are 1 answers

2
Bond - Java Bond On BEST ANSWER

Try this (note the updated value for @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);

See reference here.