【问题标题】:cURL JSON + Cookies + Header Params + JerseycURL JSON + Cookies + Header Params + Jersey
【发布时间】:2013-06-30 05:49:37
【问题描述】:

我正在尝试发送请求,我在 curl 中执行以下操作:

curl -v --header "location: 60.004:8.456" --cookie "sessionToken=~session" -i -X PUT -H 'Content-Type: application/json' -d '{"data":"{"FCT":"Welcome", "Uni":"Welcome to DI"}"}' localhost:8080/tester/apps/e39/data

由于某种原因,它匹配类但没有这个方法:

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createDocumentRoot(JSONObject inputJsonObj,
        @Context UriInfo ui, @Context HttpHeaders hh) {
}

编辑: 该类是用 @Path("{appId}/data")

定义的

问题不在于路径,因为我已经调试过它并看到它正确识别了类,它只是在进入类后抛出错误的请求而不输入任何方法。

这里是 curl 的详细说明:

* About to connect() to localhost port 8080 (#0)
*   Trying 127.0.0.1... connected
> PUT /tester/apps/e39/data HTTP/1.1
> User-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4                         libidn/1.23 librtmp/2.3
> Host: localhost:8080
> Accept: */*
> Cookie: sessionToken=~session
> location: 60.004:8.456
> Content-Type: application/json
> Content-Length: 60
> 
* upload completely sent off: 60out of 60 bytes
< HTTP/1.1 400 Bad Request
HTTP/1.1 400 Bad Request
< Server: Apache-Coyote/1.1
Server: Apache-Coyote/1.1
< Content-Type: text/html;charset=utf-8
Content-Type: text/html;charset=utf-8
< Content-Length: 990
Content-Length: 990
< Date: Tue, 02 Jul 2013 21:46:56 GMT
Date: Tue, 02 Jul 2013 21:46:56 GMT
< Connection: close
Connection: close

【问题讨论】:

  • 如果没有看到类和方法上的@Path 注释,很难判断。请您提供更多代码吗?
  • 添加了 curl 详细信息和其他一些信息。问题不在于Path注解,而在于http请求内容与方法的匹配。
  • 谢谢你,jgm,你太客气了。问题出在没有正确编写的json中,我会写一个答案,以防有人像我一样困惑。

标签: java http cookies curl jersey


【解决方案1】:

问题在于 json 语法不正确,我有

'{"data":"{"FCT":"Welcome", "Uni":"Welcome to DI"}"}'

改成这个后,效果很好:

'{"data":
{"FCT":"Welcome",
"Uni":"Welcome to DI"}}'

我使用了一个 JSON 在线解析器来检查 json 语法,这是链接以防有人需要它: http://json.parser.online.fr/

【讨论】:

    猜你喜欢
    • 2019-06-30
    • 1970-01-01
    • 2023-03-21
    • 2012-06-13
    • 2012-12-02
    • 1970-01-01
    • 2012-12-13
    • 1970-01-01
    • 2016-01-09
    相关资源
    最近更新 更多