问题是JavaHTTPUrlConnection
不支持HTTP.PATCH。
因此,我找到了一个指南,该指南使用正文执行了删除操作。但我收到HTTP/1.1 400 Bad Request
我到达了这个:
*导入 compile("org.apache.httpcomponents:httpclient:4.3.1")
方法:
@SneakyThrows
public void firebasePatch(final PARAM p) {
new TaskRunner() {
@Override
public void command() throws Throwable {
final HttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 10000);
PARAM p2 = p;
String callUrl = url+".json"+"?auth="+firebaseAuth;
HttpPatch httpPatch = new HttpPatch(callUrl);
httpPatch.addHeader("Accept", "*/*");
httpPatch.addHeader("Content-Type", "application/json");
// httpPatch.addHeader("auth", firebaseAuth);
String patchString = "{'userwallposts': {'ame_lector_dk': [{'wallPostUrl': '/wallposts/1','createdByLink': '',"+
"'summary': 'blah gik sig ikke en tur, og købte blah med hjem','createdDate': '20140126220550','comments': '2',"+
"'title': '1Blah blah blah','createdBy': 'PATCHED!','picture': 'http://images6.alphacoders.com/316/316963.jpg'}]}}";
StringEntity entity = new StringEntity(patchString, "UTF-8");
entity.setContentType("application/json");
httpPatch.setEntity(entity);
HttpResponse response = httpClient.execute(httpPatch);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200 ? true : false) {
// ok
System.out.println("patch complete");
} else {
System.out.println(response.getStatusLine());
}
}
}.run(nonBlock, exceptionHandling);
}
我检查了 Url+ 方法在 Advanced Rest 客户端中是否有效,无法弄清楚为什么我的 Spring boot 项目没有得到相同的输出,任何帮助将不胜感激。