gpt4 book ai didi

java - Spring RequestMapping 通配符和后缀

转载 作者:行者123 更新时间:2023-12-02 04:41:08 31 4
gpt4 key购买 nike

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

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

我的 Controller 中的请求映射是:

/reference/download/**

问题是,只有当我调用 不带 .pdf 后缀的 URI 时,映射才起作用。

这是我的代码:

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

}

最佳答案

这称为Content Negotiation .

关于java - Spring RequestMapping 通配符和后缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30164533/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com