Feign - URL编码路径参数

时间:2017-05-10 09:57:47

标签: spring-boot netflix-feign feign

这是我的合同,

@RequestLine("GET /products/{id}")
@Headers({"Content-Type: application/json"})
ApiResponse getProduct(@Param("id") String productId) throws Exception;

我想获取id =“a / b”,

的产品

如果我将此作为参数发送到getProduct("a/b")

然后形成的网址是http://api/products/a/b,我得到的是404而网址应为http://api/products/a%2Fb

有解决方法吗?

2 个答案:

答案 0 :(得分:2)

一个简单的配置就可以了,

@RequestLine(value = "GET /products/{id}", decodeSlash = false)
@Headers({"Content-Type: application/json"})
ApiResponse getProduct(@Param("id") String productId) throws Exception;

路径参数正确编码,但RequestTemplate在发出导致问题的请求之前再次解码URL(默认为decodeSlash = true)。

答案 1 :(得分:0)

就我而言,当代码如下所示时:

@GetMapping(path = "/document/{documentId}/files/{fileId}")
  ResponseEntity<byte[]> getDocument(@PathVariable("documentId") String documentId, @PathVariable(value = "fileId") String fileId);

还有一个问题是@PathVariable fileId 可能是 123/SGINED

设置 application.property feign.client.decodeSlash=false 有帮助。

相关问题