【发布时间】:2019-02-14 00:19:12
【问题描述】:
我无法弄清楚为什么我的 GET 端点被调用,但我的 POST 端点却不工作。当我调用curl -v -X GET http://localhost:8080/myresource/test123 时,它成功返回hello
但是当我打电话时
curl -v -X POST \
http://localhost:8080/myresource \
-H 'Content-Type: application/json' \
-d '{"test": "testvalue"}'
我不断收到这样的回复:
* Connected to localhost (::1) port 8080 (#0)
> POST /myresource HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 21
>
* upload completely sent off: 21 out of 21 bytes
< HTTP/1.1 500 Request failed.
< Content-Type: text/html;charset=ISO-8859-1
< Connection: close
< Content-Length: 1031
<
* Closing connection 0
<html><head><title>Grizzly 2.4.0</title><style><!--div.header {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#003300;font-size:22px;-moz-border-radius-topleft: 10px;border-top-left-radius: 10px;-moz-border-radius-topright: 10px;border-top-right-radius: 10px;padding-left: 5px}div.body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:#FFFFCC;font-size:16px;padding-top:10px;padding-bottom:10px;padding-left:10px}div.footer {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#666633;font-size:14px;-moz-border-radius-bottomleft: 10px;border-bottom-left-radius: 10px;-moz-border-radius-bottomright: 10px;border-bottom-right-radius: 10px;padding-left: 5px}BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;}B {font-family:Tahoma,Arial,sans-serif;color:black;}A {color : black;}HR {color : #999966;}--></style> </head><body><div class="header">Request failed.</div><div class="body">Request failed.</div><div class="footer">Grizzly 2.4.0</div></body></html>%
这是我的代码
import javax.ws.rs.*
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.Response
@Path("myresource")
class HelloWorldResource {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
fun createMessage(testPost: String): Response {
return Response.status(200).entity("helllo post").build()
}
@GET
@Path("{testGet}")
@Produces(MediaType.APPLICATION_JSON)
fun getMessage(@PathParam("testGet") testGet: String): Response {
return Response.status(200).entity("hello").build()
}
}
【问题讨论】:
-
JAX-RS 的 5xx 错误表示某种异常被抛出。你有那个例外吗?
-
这可能是由于
createMessage采用String,但您的 POST 正文包含可能通过一个test字段编组到 POJO 的 JSON。 -
在您的应用程序中注册this DebugExceptionMapper。您很可能会看到被容器吞噬的异常。如果在看到异常后,您无法弄清楚如何修复它,请在此处发布异常/堆栈跟踪。
标签: kotlin jax-rs jersey-1.0