2017-07-15 114 views
0

如何使用JAVA中的HttpURLConnection執行RESTHeart API的PATCH請求。它將405返回爲狀態。在JAVA中使用HttpURLConnection的PATCH請求

public class TestRestHeartUpdate{ 
public static void main(String[] args) throws Exception{ 

URL url; 
url = new 
URL("http://127.0.0.1:8080/testDB1/col1/5967c13e61f73b88018db783"); 

HttpURLConnection hc; 
hc = (HttpURLConnection) url.openConnection(); 
hc.setDoInput(true); 
hc.setDoOutput(true); 
hc.setRequestMethod("POST"); 
hc.setRequestProperty("X-HTTP-Method-Override", "PATCH"); 

hc.setRequestProperty("Content-Type","application/hal+json"); 
hc.setRequestProperty("Accept","application/hal+json"); 
hc.setInstanceFollowRedirects(false); 
hc.setRequestProperty("If-Match","5967c28f42483b1c3dbedc54"); 
hc.setUseCaches(false); 
hc.connect(); 

byte[] opB = " {'rating':'VeryGood'}".getBytes("UTF-8"); 
OutputStream os = hc.getOutputStream(); 
os.write(opB); 
System.out.println(hc.getResponseCode()); 
} 
} 

回答

0

HTTP代碼405表示方法不允許。這表明您的Web服務器不允許PATCH請求,並且您需要將其配置爲接受PATCH方法類型。

+0

你能告訴我如何配置服務器接受PATCH方法類型 –

相關問題