我正在使用Resteasy客户端代理框架与REST API进行通信。我已经在代理接口上定义了一些方法,但不幸的是,我在发送http请求时遇到了一些与Content-Type无法正确设置的问题,这给了我一个http 400(错误请求)错误代码,因为远程API期待内容-type header param是application / json。
这是它的样子:
@Path("/")
public interface RestAPIClient {
@POST
@Path("/send")
String send(@CookieParam("token") String token, @FormParam("name") String id, @FormParam("message") String message, @FormParam("emails") List<String> emails);
}
有没有办法直接在代理接口级别将“Content-type”标头设置为“application / json”?使用一些注释可能吗?
谢谢
我试试了
@Consumes(MediaType.APPLICATION_JSON)
在POST?
注意许多事情:你需要有一个派生方法的“实体”对象(不是@CookieParam或@FormParam)。并在
所以你的方法必须像:
@POST
@Path("/auth")
@Consumes(MediaType.APPLICATION_JSON)
ConnectionInformation auth(@CookieParam("name") String name, @CookieParam("password") String password, MyJSONObject entity);
而“实体”将产生
Content-Type: application/json
你需要。
以及使用:
@Consumes(MediaType.APPLICATION_JSON)
你需要确保请求正文中确实有内容(不仅仅是params)。
如果没有内容,则不会设置Content-Type标头。