我必须在java中使用PATCH API。当我尝试在下面的代码中使用时,它会给出异常
java.net.ProtocolException: Invalid HTTP method: PATCH
at java.net.HttpURLConnection.setRequestMethod(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.setRequestMethod(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.setRequestMethod(Unknown Source)
at com.java.jackson.jackson.PatchRequest.main(PatchRequest.java:32)
下面代码中的这一行引发异常。
connection.setRequestMethod("PATCH");
我正在使用以下代码进行相同的操作。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject;
public class PatchRequest {
public static void main(String[] args) {
try {
HttpURLConnection connection=null;
String targetUrl="https://xxxxxxxxx-t0.data.digitalassistant.oci.oc-test.com/api/v1/bots/"+botID+"/dynamicEntities/"+dynamicEntityId+"/pushRequests/"+pushRequestId+"/values";
URL url=new URL(targetUrl);
connection=(HttpURLConnection) url.openConnection();
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("PATCH");
connection.setRequestProperty("Content-Type","application/json");
connection.setRequestProperty("Authorization","Bearer "+"eyJ4NXQjUzI1NiI6IjZhYnBycWVsRl9ld3NEaW1HRy0wY0JpNVg2N09jQjkzTGNyN2Y4M3UzLXMiLCJ4NXQiOiJkdlhSRXBGdXpTenRMdGlMM2Vmbk1rbVo0Rk0iLCJraWQiOiJTSUdOSU5HX0tFWSIsImFsZyI6IlJTMjU2In0.eyJzdWIiOiJpZGNzLW9kYS05NDE3ZjkzNTYwYjk0ZWI4YTJlMmE0YzlhYWM5YTNmZi10MF9BUFBJRCIsInVzZXIudGVuYW50Lm5hbWUiOiJpZGNzLTgyOTcyOTIxZTQyNjQxYjFiZjA4MTI4YzNkOTNhMTljIiwic3ViX21hcHBpbmdhdHRyIjoidXNlck5hbWUiLCJwcmltVGVuYW50Ijp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Gfn56VOJvxshQAVjfCrMyGS6FeTlnuR-4CLEwy_QYWXV3CX7JRN2cVbYapwS1Nex9KX0wrF-tMmgEEttBsQxDlc6BqwacCtFBVjKvZTyhucivjQCfMhFINeMtL3ktqs1e3_ztundyiWV6vjDgrGjVY5UatHLqdorTC0Q56GVi3OPIAKisDUAP7vtnaeuaLR9aARwPVDFLcY6gMjsOWMYCfAHkAZ7RBkuLsptxUc3czUXfqMLxqtw");
connection.setRequestProperty("Accept", "application/json");
OutputStream os = connection.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
osw.write(""{\n \"delete\": [\n \n {\n \"canonicalName\": \"FTE Discrete Org\"\n } \n ],\n \"modify\": [\n {\n \"canonicalName\": \"FTE Process Org\",\n \"synonyms\": [\n \"FTE Process Org\",\"FTE Process Org\",\"fteprocessorg\"\n ]\n }\n ],\n \n \"add\": [ \n\n {\n \"canonicalName\": \"Vision Canada\",\n \"synonyms\": [\n \"visioncanada\",\n \"vision canada\",\"vision canada\"\n ]\n },\n {\n \"canonicalName\": \"apple computers\",\n \"synonyms\": [\n \"apple corp\",\n \"apple\" \n ]\n }\n ]\n}"");
osw.flush();
osw.close();
os.close();
System.out.println("Code works before connect");
connection.connect();
System.out.println("Code works after connect");
int responseCode=connection.getResponseCode();
System.out.println("Response code:"+responseCode);
if (responseCode==401) {
System.out.println("Unauthorized Error");
} else if(responseCode==200) {
InputStreamReader in=new InputStreamReader(connection.getInputStream());
BufferedReader reader =new BufferedReader(in);
String output;
String apiOutput="";
while((output=reader.readLine())!=null) {
apiOutput+=output;
}
System.out.println(apiOutput);
connection.disconnect();
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
我在谷歌中阅读,然后我用低于 2 行替换了引发异常的行。
connection.setRequestProperty("X-HTTP-Method-Override", "PATCH");
connection.setRequestMethod("POST");
然后它抛出Http status code 412
.你能帮我同样的忙吗?
您将无法使用java.net.HttpURLConnection
在下面的链接中查看详细信息。您需要在java.net.http
包中使用客户端 API。
详细信息可以在这里找到:https://bugs.openjdk.java.net/browse/JDK-8207840