简体   繁体   English

如何在JAX-RS中向GET请求添加主体

[英]How to add a body to a GET request in JAX-RS

I'm trying to consume a REST API that requires a body with a GET request. 我正在尝试使用需要具有GET请求的正文的REST API。 But as a GET usually doesn't have a body, I can't find a way to attach a body in my request. 但由于GET通常没有身体,我找不到在我的请求中附加身体的方法。 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). 我也在构建REST API,但教授不允许我们将方法更改为POST(他给了我们一个我们要创建的端点列表,不多也不少)。

I'm trying to do it like this: 我试着这样做:

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

Where I set the method to GET and the body to my get body . 我将method设置为GET ,将bodymy get body However, using this approach I get an exception: 但是,使用这种方法我得到一个例外:

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

Is there any way to do this with JAX-RS? 有没有办法用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. 我们学会了使用JAX-RS所以我更喜欢使用这个解决方案,因为我不确定我的教授会允许我们使用任何其他REST客户端。 I'm currently using RESTEasy, provided by the WildFly server. 我目前正在使用WildFly服务器提供的RESTEasy。

(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.) (这不是HTTP GET与请求主体的重复,因为我在询问如何在JAX-RS中使用body创建GET请求,而不是应该这样做。)

This depends on what is your JAX-RS implementation. 这取决于您的JAX-RS实现。 This check can be disabled in Jersey 2.25 using SUPPRESS_HTTP_COMPLIANCE_VALIDATION property: 可以使用SUPPRESS_HTTP_COMPLIANCE_VALIDATION属性在Jersey 2.25中禁用此检查:

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"));

Instead of exception you will get an INFO log 而不是异常,您将获得INFO日志

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

However in RESTEasy 3.5.0.Final there is a hardcoded check in both URLConnectionEngine and ApacheHttpClient4Engine : 但是在RESTEasy 3.5.0.Final中, URLConnectionEngineApacheHttpClient4Engine都有一个硬编码检查:

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

You would have to create your own implementation of the ClientHttpEngine to skip this. 您必须创建自己的ClientHttpEngine实现才能跳过此步骤。 Then you need to supply it when building the client: 然后你需要在构建客户端时提供它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM