我不知道为什么我的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()
}
}
答案 0 :(得分:0)
虽然看不到实际的潜在异常,但是很难确定,但是很可能您遇到了Jersey: Return a list of strings类似的问题,其中String
类型和MediaType.APPLICATION_JSON
类型之间不匹配/消耗声明。如果您要处理原始字符串,建议您使用MediaType.PLAIN_TEXT,或者将您的帖子正文和返回值设置为一个可以表示为非原始json对象的实体(即,{{ 1}}),并确保已向球衣注册了杰克逊提供者。