所以,我試圖創建一個簡單的(我的意思是簡單)POST請求。這是服務器端的類。POST請求到一個REST服務器
@Stateless
@Path("cards")
public class CardsFacadeREST extends AbstractFacade<Cards> {
@POST
@Path("test")
@Consumes({"text/plain"})
public void createTestCard() {
Cards card = new Cards();
card.setName("Test Card");
super.create(card);
}
@GET
@Path("count")
@Produces("text/plain")
public String countREST() {
return String.valueOf(super.count());
}
}
GET方法工作正常,但POST方法不適用於我。我正在使用Chrome的高級休息客戶端。
- 網址爲http://localhost:8080/dc-rest/webresources/cards/test
- 類型是POST
- 我的頭就是像這樣:內容類型:text/plain的
就是這樣。
我不斷收到「400:錯誤的請求,客戶端發送的請求在語法上不正確。」
當我打開的窗口JSON的響應,所有它說是「意外令牌<」
這裏是請求頭,如果有什麼差別。
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Content-Type: text/plain
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cookie: JSESSIONID=f4c746a32b46244d422800192f04; treeForm_tree- hi=treeForm:tree:applications
Body is empty.
和響應:
X-Powered-By: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition 4.0 Java/Oracle Corporation/1.7)
Server: GlassFish Server Open Source Edition 4.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Allow: GET,DELETE,OPTIONS,PUT,POST
Access-Control-Allow-Headers: content-type
Content-Language:
Content-Type: text/html Date: Thu, 11 Feb 2016 20:48:12 GMT
Connection: close Content-Length: 1105
Body:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>GlassFish Server Open Source Edition 4.0 - Error report</title><style type="text/css"><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 400 - Bad Request</h1><hr/><p><b>type</b> Status report</p><p><b>message</b>Bad Request</p><p><b>description</b>The request sent by the client was syntactically incorrect.</p><hr/><h3>GlassFish Server Open Source Edition 4.0 </h3></body></html>
您可以發佈您正在使用的客戶端的請求和響應數據嗎?整個事情。網址,動詞,請求和響應主體,標題,作品。 –
有點麻煩,但我認爲它全部在那裏。 – exxodus7
Man,這個API似乎並不能幫助你弄清楚爲什麼你會得到這個錯誤。我可以告訴你,你得到的「Unexpected token <」錯誤是因爲Json序列化程序期待JSON,並且它正在獲取HTML(xml)。它會拋出該異常,因爲它會在響應中看到的起始字符是「<」而不是「{」或「[」。現在我不知道它爲什麼會返回一個400.你可以嘗試以下方法:確保設置了content-length,確保accept和content-type設置爲application/json(我假設服務器以JSON表示) –