如何在JAX-RS中将主体添加到GET请求 [英] How to add a body to a GET request in JAX-RS

查看:247
本文介绍了如何在JAX-RS中将主体添加到GET请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用一个REST API,该API需要带有GET请求的主体.但是由于GET通常没有主体,因此我找不到在请求中附加主体的方法.我也在构建REST API,但是教授不允许我们将方法更改为POST(他向我们提供了我们要创建的端点的列表,更多,不少).

I'm trying to consume a REST API that requires a body with a GET request. But as a GET usually doesn't have a body, I can't find a way to attach a body in my request. I am also building the REST API, but the professor won't allow us to change the method to POST (he gave us a list of the endpoints we are to create, no more, no less).

我正在尝试这样做:

Response r = target.request().method(method, Entity.text(body));

我将method设置为GET并将body设置为my get body的位置.但是,使用这种方法会出现异常:

Where I set the method to GET and the body to my get body. However, using this approach I get an exception:

javax.ws.rs.ProcessingException: RESTEASY004565: A GET request cannot have a body.

JAX-RS有什么方法可以做到这一点吗?我们学会了使用JAX-RS,因此我更喜欢使用此解决方案,因为我不确定我的教授是否允许我们使用任何其他REST客户端.我当前正在使用WildFly服务器提供的RESTEasy.

Is there any way to do this with JAX-RS? We learned to use JAX-RS so I would prefer a solution using this, as I'm not sure my professor would allow us to use any other REST client. I'm currently using RESTEasy, provided by the WildFly server.

(这不是带有请求正文的HTTP GET 的重复,因为我我问如何在JAX-RS中使用body创建GET请求,而不是是否应该这样做.)

(This is not a duplicate of HTTP GET with request body because I'm asking on how to create a GET request with body in JAX-RS, not if it should be done.)

推荐答案

这取决于您的JAX-RS实现.可以使用SUPPRESS_HTTP_COMPLIANCE_VALIDATION属性在泽西岛2.25中禁用此检查:

This depends on what is your JAX-RS implementation. This check can be disabled in Jersey 2.25 using SUPPRESS_HTTP_COMPLIANCE_VALIDATION property:

ClientConfig config = new ClientConfig();
config.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
JerseyClient client = JerseyClientBuilder.createClient(config);
WebTarget target = client.target(URI.create("https://www.stackoverflow.com"));
Response response = target.request().method("GET", Entity.text("BODY HERE"));

您将获得一个INFO日志,而不是异常

Instead of exception you will get an INFO log

INFO: Detected non-empty entity on a HTTP GET request. The underlying HTTP transport connector may decide to change the request method to POST.

但是在RESTEasy 3.5.0中,最终在URLConnectionEngineApacheHttpClient4Engine中都进行了硬编码检查:

However in RESTEasy 3.5.0.Final there is a hardcoded check in both URLConnectionEngine and ApacheHttpClient4Engine:

if (request.getEntity() != null)
{
  if (request.getMethod().equals("GET")) throw new ProcessingException(Messages.MESSAGES.getRequestCannotHaveBody());

您必须创建自己的ClientHttpEngine实现以跳过此过程.然后,在构建客户端时需要提供它:

You would have to create your own implementation of the ClientHttpEngine to skip this. Then you need to supply it when building the client:

ClientHttpEngine engine = new MyEngine();
ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();

这篇关于如何在JAX-RS中将主体添加到GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆