|
我使用 Ubuntu 并安装在上面cURL。我想用 cURL 测试我的 Spring REST 应用程序。我在 Java 端写了我的 POST 代码。但是,我想用 cURL 测试它。我正在尝试发布 JSON 数据。示例数据如下:' N' t' d" \% I; V
{"value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true}2 F1 I. {3 d' K* K) _/ u8 t6 J
我用这个命令:
( ^6 ~' n' W7 V' D p1 Z j3 Q6 ^curl -i \ -H "Accept: application/json" \ -H "X-HTTP-Method-Override: PUT" \ -X POST -d "value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true \ http://localhost:8080/xx/xxx/xxxx- H- Y5 g& S% Y( H0 H/ b8 d6 X9 L
回到这个错误:
. t$ b! D. C% y: ^9 _+ {HTTP/1.1 415 Unsupported Media TypeServer: Apache-Coyote/1.1Content-Type: text/html;charset=utf-8Content-Length: 1051Date: Wed,24 Aug 2011 08:50:17 GMT
# _* ^, z* l. S4 S4 `: N 错误描述如下:$ b* K( |# ]2 X' X
由于请求实体的格式不受请求方法 () 请求资源的支持,服务器拒绝了此请求。: Q& K2 S9 z$ d& P! L% s0 o% ?0 B0 N
Tomcat 日志:“POST /ui/webapp/conf/clear HTTP/1.1”415 10511 v7 u7 O( R. v' }
cURL 命令的正确格式是什么?+ J" R# z/ x2 N5 \- x' W9 c5 [+ }
这是我的 Java 端PUT代码(我已经测试GET 和 DELETE 他们可以工作):
" a5 A/ F% {5 _" o@RequestMapping(method = RequestMethod.PUT)public Configuration updateConfiguration(HttpServletResponse response,@RequestBody Configuration configuration) { //consider @Valid tag configuration.setName(" UT worked"); todo If error occurs response.sendError(HttpServletResponse.SC_NOT_FOUND); return configuration;}; f" T7 k* z5 E7 ~$ B
% A+ O4 {8 E4 x' i' P) d7 N4 g 解决方案:
3 }" y# |. V( ~' V0 q+ \ 您需要将内容类型设置为 application/json。但是-d(或--data)发送 Content-Type application/x-www-form-urlencoded,这在 Spring 方面是不被接受的。' w2 ], G0 g& x/ ]4 Y
查看curl 手册页,我想你可以用-H(或--header):
; A) i4 {. u1 z5 V# Z5 ?-H "Content-Type: application/json"
6 a6 A4 W% F9 F ~; w# l4 z. O& T 完整示例:
/ K/ c/ i( H2 s! E& _- b8 d% }, N% Ccurl --header "Content-Type: application/json" \ --request POST \ --data '{"username":"xyz","password":"xyz"}' \ http://localhost:3000/api/login. ^7 g! L) s8 o% n# [) P2 \
(-H是--header,-d的缩写--data)4 `& L1 `& \3 `3 O: N* f4 V
请注意,如果你使用它,这个-request POST是可选-d的,因为该-d标志意味着 POST 请求。: N D& t* X( [- G
在 Windows 上,情况略有不同。请参阅评论线程。 |
|