Spring RequestMapping通配符和后缀

时间:2015-05-11 09:52:46

标签: java spring spring-mvc request

我想为这样的URI创建一个映射。

  

/reference/download/asdsa/asdas/sad/3c7d38f679a64d101c602da61dad9912.pdf

我的控制器中的请求映射是:

  

/参考/下载/ **

问题是,只有当我调用URI 而没有 .pdf后缀时,映射才有效。

这是我的代码:

@Controller
@RequestMapping("/reference")
public class ReferenceFilePageController
{
    private static final Logger LOG = Logger.getLogger(ReferenceFilePageController.class);

    private static final String PATH_VARIABLE_PATTERN_DOWNLOAD = "/download/**";

    @Autowired
    private ReferenceFileService referenceFileService;

    @RequestMapping(value = PATH_VARIABLE_PATTERN_DOWNLOAD, method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @ResponseBody
    public FileSystemResource getFile(HttpServletRequest request) {
        String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);

        return new FileSystemResource(referenceFileService.getPathForIdentifier(path.substring(19)));
    }

}

1 个答案:

答案 0 :(得分:1)

这就是所谓的内容协商,请看一下这个链接:https://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc