英文:
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<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());
}
});
}
How do I get my webTarget to send a request in Patch mode?
答案1
得分: 0
你可以使用method
方法,将第一个参数设置为"PATCH"。
method
是一个通用方法(与post
、get
、delete
相对,它们的HTTP方法是硬编码的),允许你调用任意方法:
webTarget.request(mediaType).headers(headers).method("PATCH", entity);
有关更多详细信息,请参阅文档:WebTarget。WebTarget.request
会为你提供一个Invocation.Builder的实例,它继承自SyncInvoker中的method
方法。
注意:根据你使用的JDK版本和JAX-RS库,调用PATCH方法时可能会遇到问题。如果遇到问题,可以查看以下链接是否有帮助:
- https://stackoverflow.com/questions/22355235/patch-request-using-jersey-client
- https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch
- https://stackoverflow.com/questions/42026770/invalid-http-method-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("PATCH", 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,"true");
PATCH method only works when SET_METHOD_WORKAROUND is true.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论