相关疑难解决方法(0)

SpringMVC:取决于url扩展名的映射行为不一致

我有一个基于RESTful spring的端点,可以将存储在db中的资源存储到javascript编辑器中.相关部分归结为:

@RestController
@RequestMapping(ThemeEndpoint.ENDPOINT_NAME)
public class ThemeEndpoint {

public static final String ENDPOINT_NAME = "/themes"; 

    @RequestMapping(value="/{id}/css/{assetName:.*}", method=RequestMethod.GET)
    public Asset getCssItem(
        @PathVariable("id") ThemeId id, 
        @PathVariable("assetName") String name) {
    CssThemeAsset themeAsset = themeService.getCssAsset(
                id, ThemeAssetId.fromString(name));
    Asset asset = new Asset();
    asset.name = themeAsset.getName();
    asset.text = themeAsset.getContent();
    return asset;
}
Run Code Online (Sandbox Code Playgroud)

对于像这样的网址,这可以正常工作

http://localhost:8080/app-url/rest/themes/ac18a080-a2f1-11e3-84f4-600308a0bd14/css/main.less
Run Code Online (Sandbox Code Playgroud)

但是一旦我将扩展名更改为,就会失败.css.

经过一些调试后,我非常确定如果我使用的是url,请求甚至都没有映射

http://localhost:8080/app-url/rest/themes/ac18a080-a2f1-11e3-84f4-600308a0bd14/css/main.css
Run Code Online (Sandbox Code Playgroud)

对于高日志级别,我可以看到映射是由spring捕获的:

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping 
    - Mapped "{[/themes/{id}/css/{assetName:.*}],methods=[GET],params=[],headers=[],
                consumes=[],produces=[application/json],custom=[]}" 
      onto public xxx.endpoint.ThemeEndpoint$Asset
          xxx.endpoint.ThemeEndpoint.getCssItem(
              net.lacho.svc.themes.api.ThemeId,java.lang.String)   
Run Code Online (Sandbox Code Playgroud)

并且使用非.css扩展名调用控制器:

Found 1 matching mapping(s) for [/themes/ac18a080-a2f1-11e3-84f4-600308a0bd14/css/main.less]
  : [{[/themes/{id}/css/{assetName:.*}],methods=[GET],params=[],headers=[],
        consumes=[],produces=[application/json],custom=[]}]
Run Code Online (Sandbox Code Playgroud)

但是只要我使用.css作为扩展名 - bang:

Looking up …
Run Code Online (Sandbox Code Playgroud)

spring-mvc spring-4

9
推荐指数
1
解决办法
2421
查看次数

Spring不会忽略文件扩展名

在我的Spring XML中,我有以下代码段:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="useDefaultSuffixPattern" value="false"/>
</bean>

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
            <property name="objectMapper" ref="objectMapper" />
        </bean>        
    </mvc:message-converters>
</mvc:annotation-driven>
Run Code Online (Sandbox Code Playgroud)

据我了解,这意味着春天应该登记"ABC*"和"ABC /"当我有"ABC"的映射.

在我的一个控制器中,我有一个将图像写入响应的方法:

@RequestMapping(value="{path}", method=RequestMethod.GET, produces=MediaType.IMAGE_PNG_VALUE)
@ResponseBody
public void getPath(
        @PathVariable String path,
        HttpServletResponse res) {

    ...
}
Run Code Online (Sandbox Code Playgroud)

当我请求像"abc"这样的东西时,这很有用,但是当我请求"abc.com"时,它会在文本中抛出406错误:

The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers."
Run Code Online (Sandbox Code Playgroud)

当我请求"abc.img"时,"path"参数只接收文本"abc"; Spring省略了扩展名.

看来Spring似乎没有正确地忽略后缀模式.为什么是这样?

编辑:我从Dirk的评论中翻译了java配置,以下XML似乎解决了这个问题:

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

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
    <mvc:message-converters>
        <bean …
Run Code Online (Sandbox Code Playgroud)

java spring uri spring-mvc http-status-code-406

4
推荐指数
1
解决办法
4885
查看次数

标签 统计

spring-mvc ×2

http-status-code-406 ×1

java ×1

spring ×1

spring-4 ×1

uri ×1