【问题标题】:Why POST endpoint not being invoked but GET endpoint is being invoked - jersey container grizzly2为什么没有调用 POST 端点但调用了 GET 端点 - jersey 容器 grizzly2
【发布时间】: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


【解决方案1】:

没有看到实际的底层异常,很难确定,但您可能会遇到与 Jersey: Return a list of strings 类似的问题,String 类型和 MediaType.APPLICATION_JSON 生产/消费声明之间不匹配。如果您正在处理原始字符串,我建议您使用 MediaType.PLAIN_TEXT,或者让您的帖子正文和返回值是一个可以表示为非原始字符串 json 对象的实体(即包含在 @987654324 中的东西@),并确保在 jersey 上注册了 jackson 提供程序。

【讨论】:

    猜你喜欢
    • 2016-09-13
    • 1970-01-01
    • 2019-05-15
    • 2023-01-19
    • 1970-01-01
    • 2019-09-26
    • 2021-07-28
    • 2019-06-21
    • 2019-07-06
    相关资源
    最近更新 更多