如何以 Patch 模式发送 restRequest?

huangapple go评论76阅读模式
英文:

How to send restRequest in Patch mode?

问题

以下是翻译好的内容:

我在使用 Postman,我看到它有许多模式。我已经能够实现一个 restRequest 对象,它“知道”如何以 Post 或 Get 方法发送请求。

这是我的代码的一部分:

@Override
public RestResponse sendRequest() {
    return data.accept(new RequestDataVisitor<RestResponse>() {
        @Override
        public RestResponse visit(GetData getData) {
            return new RestResponse(webTarget.request().headers(headers).get());
        }

        @Override
        public RestResponse visit(PostFormData post) {
            return new RestResponse(webTarget.request(post.getMediaType()).headers(headers).post(post.getEntity()));
        }

        @Override
        public RestResponse visit(PostRawData post) {
            return new RestResponse(webTarget.request(post.getMediaType()).headers(headers).post(post.getEntity()));
        }

        @Override
        public RestResponse visit(DeleteData deleteData) {
            return new RestResponse(webTarget.request(deleteData.getMediaType()).headers(headers).delete());
        }
        
        // 添加如下内容来支持 Patch 请求
        @Override
        public RestResponse visit(PatchData patchData) {
            return new RestResponse(webTarget.request(patchData.getMediaType()).headers(headers).method("PATCH", patchData.getEntity()));
        }
    });
}

如何让我的 webTarget 以 Patch 模式发送请求?

英文:

I'm working with Postman and I see that it has many modes. I was able to implement a restRequest object that "knows" how to send a request in Post or Get method.

This is part of my code:

 @Override
public RestResponse sendRequest() {
    return data.accept(new RequestDataVisitor&lt;RestResponse&gt;() {
        @Override
        public RestResponse visit(GetData getData) {
            return new RestResponse(webTarget.request().headers(headers).get());
        }

        @Override
        public RestResponse visit(PostFormData post) {
            return new RestResponse(webTarget.request(post.getMediaType()).headers(headers).post(post.getEntity()));
        }

        @Override
        public RestResponse visit(PostRawData post) {
            return new RestResponse(webTarget.request(post.getMediaType()).headers(headers).post(post.getEntity()));
        }

        @Override
        public RestResponse visit(DeleteData deleteData) {
            return new RestResponse(webTarget.request(deleteData.getMediaType()).headers(headers).delete());

        }
    });
}

How do I get my webTarget to send a request in Patch mode?

答案1

得分: 0

你可以使用method方法,将第一个参数设置为"PATCH"。

method是一个通用方法(与postgetdelete相对,它们的HTTP方法是硬编码的),允许你调用任意方法:

webTarget.request(mediaType).headers(headers).method("PATCH", entity);

有关更多详细信息,请参阅文档:WebTargetWebTarget.request会为你提供一个Invocation.Builder的实例,它继承自SyncInvoker中的method方法。

注意:根据你使用的JDK版本和JAX-RS库,调用PATCH方法时可能会遇到问题。如果遇到问题,可以查看以下链接是否有帮助:

英文:

You can use the method method with first argument set to "PATCH".

method is a generalized method (as opposed to post, get, delete which have the http-method hardwired) and allows you to call an arbitrary method:

webTarget.request(mediaType).headers(headers).method(&quot;PATCH&quot;, entity);

See the documentation for additional details: WebTarget.request gives you an instance of an Invocation.Builder which inherits method from SyncInvoker.
<br>
<br>

Note: depending on the JDK version and the JAX-RS library that you are using, you may run into problems when calling the PATCH method. If you do, see if any of these help:

答案2

得分: 0

问题出在WebTarget对象中:

WebTarget target = client.target(baseUrl).path(resourcePath)
.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND,"true");

只有在SET_METHOD_WORKAROUND为true时,PATCH方法才起作用。

英文:

The issue was in WebTarget object:

WebTarget target = client.target(baseUrl).path(resourcePath)
.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND,&quot;true&quot;);

PATCH method only works when SET_METHOD_WORKAROUND is true.

huangapple
  • 本文由 发表于 2020年10月13日 18:21:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64333331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定