Feign - URL 编码路径参数
Feign - URL encode path params
这是我的合同,
@RequestLine("GET /products/{id}")
@Headers({"Content-Type: application/json"})
ApiResponse getProduct(@Param("id") String productId) throws Exception;
我想获取 id = "a/b",
的产品
如果我将其作为参数发送给 getProduct("a/b")
然后形成的 URL 是 http://api/products/a/b
而我得到一个 404 而不是 url 应该是 http://api/products/a%2Fb
有办法解决这个问题吗?
一个简单的配置就可以做到,
@RequestLine(value = "GET /products/{id}", decodeSlash = false)
@Headers({"Content-Type: application/json"})
ApiResponse getProduct(@Param("id") String productId) throws Exception;
路径参数已正确编码,但 RequestTemplate 在发送导致问题的请求之前再次解码 URL(默认情况下 decodeSlash=true)。
在我的例子中,当代码如下所示时:
@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
有帮助。
这是我的合同,
@RequestLine("GET /products/{id}")
@Headers({"Content-Type: application/json"})
ApiResponse getProduct(@Param("id") String productId) throws Exception;
我想获取 id = "a/b",
的产品如果我将其作为参数发送给 getProduct("a/b")
然后形成的 URL 是 http://api/products/a/b
而我得到一个 404 而不是 url 应该是 http://api/products/a%2Fb
有办法解决这个问题吗?
一个简单的配置就可以做到,
@RequestLine(value = "GET /products/{id}", decodeSlash = false)
@Headers({"Content-Type: application/json"})
ApiResponse getProduct(@Param("id") String productId) throws Exception;
路径参数已正确编码,但 RequestTemplate 在发送导致问题的请求之前再次解码 URL(默认情况下 decodeSlash=true)。
在我的例子中,当代码如下所示时:
@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
有帮助。