簡體   English   中英

Curl不適用於HTTP發布到Spring數據休息的RequestMapping

[英]Curl not working for HTTP Post to Spring-data-rest RequestMapping

我在spring @Controller有一個RequestMapping設置,但無法正確向它發送CURL請求。 嘗試發送各種方式時,我一直收到HTTP 400返回代碼。 我不確定設置錯誤。

這是要求

curl -v -X POST localhost:8082/api/registerDevice -d '{"serial":"FA541YJ05065"}' -H "Content-Type:application/json;charset=UTF-8"

這是輸出

Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying ::1...
* Connected to localhost (::1) port 8082 (#0)
> POST /api/registerDevice HTTP/1.1
> Host: localhost:8082
> User-Agent: curl/7.49.0
> Accept: */*
> Content-Type:application/json;charset=UTF-8
> Content-Length: 23
>
* upload completely sent off: 23 out of 23 bytes
< HTTP/1.1 400 Bad Request
< Server: Apache-Coyote/1.1
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Thu, 16 Jun 2016 02:48:50 GMT
< Connection: close
<
{"timestamp":1466045330556,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Could not read document: Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: java.io.PushbackInputStream@3bae0839; line: 1, column: 2]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: java.io.PushbackInputStream@3bae0839; line: 1, column: 2]","path":"/api/registerDevice"}* Closing connection 0

春季RequestMapping

 @RequestMapping(value = "registerDevice", method = RequestMethod.POST)
  public ResponseEntity<String> registerDevice(@RequestBody Map<String, Object> payload) throws Exception {

更新資料

如下所述,這是轉義引號和與Windows相關的問題。 相同的CURL命令在centos上運行良好。

curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d "{"""serial""":"""FA541YJ05065"""}" http://localhost:8082/api/deregisterDevice

在Centos上

curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"serial":"FA541YJ05065"}' http://localhost:8082/api/registerDevice

造成這種情況的唯一原因是您的請求格式不正確。請查看此Spring 4.x / 3.x(Web MVC)REST API和JSON2 Post請求,如何一次正確地處理它? 欲獲得更多信息。

我懷疑您最終在json的開頭發送了' 您可以嘗試在json周圍使用雙引號並將其轉義。 也可以看看

curl -v -X POST localhost:8082/api/registerDevice -d "{\"serial\":\"FA541YJ05065\"}" -H "Content-Type:application/json;charset=UTF-8"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM