在请求正文中发送原始数据

时间:2018-12-19 14:59:11

标签: java spring api http netflix-feign

我遇到了将请求正文作为原始文本发送而没有引号的问题。

请求的内容类型应为 text / uri-list

在Postman中发送它可以正常工作,但是当我尝试在java中实现相同的操作时,则不起作用。

我使用假装作为api客户端。

客户端的客户端定义如下

@RequestLine("PUT /someEndpointOne/{id}/someEndpointTwo")
@Headers("Content-Type: text/uri-list")
JSONObject addSomethingToSomething(@Param("id") String id, @RequestBody okhttp3.RequestBody uri); 

我像这样在测试中使用它:

somethingClient.addSomethingToSomething("1", okhttp3.RequestBody.create(okhttp3.MediaType.parse("text/uri-list"), "http://localhost/someEndpointTwo/1"))

实际上不是发送原始数据,而是发送空对象:

  

PUT http://localhost/someEndpointOne/1/someEndpointTwo HTTP / 1.1

     

Content-Type:文本/ uri-list

     

内容长度:2

     

{}

     

END HTTP(2字节的正文)

造成不良响应的原因。

感谢您为解决此问题所提供的帮助。

1 个答案:

答案 0 :(得分:0)

以下对我有用:

@PutMapping(path = "/someEndpointOne/{id}/someEndpointTwo", headers = "Content-Type=text/uri-list")
JSONObject addSomethingToSomething(@PathVariable("id") String id, @RequestBody okhttp3.RequestBody uri); 

@PutMapping来自org.springframework.web.bind.annotation.PutMapping,而@PathVariable来自org.springframework.web.bind.annotation.PathVariable