java - 为什么我在 Jersey REST API 上发布 POST 时收到 500 响应?

标签 java jersey dropwizard postman

我刚刚开始实现我的第一个 Drowizard 项目。这是我的第一个资源类:

@Path("/bill")
public class CommandResource {
    //...

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Path("/createBillForStudy")
    public Response handleCreateBillForStudyCommand(CreateBillForStudyCommand cmd, @Context UriInfo uriInfo)
    {
        System.out.println("Creating bill for command: " + cmd);

        UUID newId = billCreator.handle(cmd);

        URI location = uriInfo.getBaseUriBuilder().path(CommandResource.class).path("/" + newId).build();

        return Response.accepted(cmd).contentLocation(location).build();
    }
}

我想使用 Postman 进行测试,但以下请求导致 500 响应,我不明白为什么:

POST /bill/createBillForStudy HTTP/1.1
Host: localhost:8080
Content-Type: application/JSON
Cache-Control: no-cache

{ "idAccount": "123", "idStudy": "456", "timeStamp": "2014-01-01" }

这是我在 Dropwizard 控制台中看到的内容:

ERROR [2015-05-07 16:43:08,558] org.glassfish.jersey.message.internal.WriterInterceptorExecutor: MessageBodyWriter not found for media type=application/xml, type=class com.statista.billing.domain.commands.CreateBillForStudyCommand, genericType=class com.statista.billing.domain.commands.CreateBillForStudyCommand.
0:0:0:0:0:0:0:1 - - [07/Mai/2015:16:43:08 +0000] "POST /bill/createBillForStudy/ HTTP/1.1" 500 332 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36" 8

对我来说,这听起来好像 Content-Type header 被扭曲或丢失,但正如您在上面看到的,它被正确设置为“application/JSON”。

有什么想法吗?

最佳答案

Response.accepted(Object) 。这会将表示返回给客户端。为了确定响应的 Content-Type,我们需要指定它,它应该由 Accept 请求 header 指定。

如果您要返回表示形式,则应始终确保使用 @Produces 注释指定支持的格式。如果您想返回 JSON(与接受 JSON 的方式相同,则只需使用

@Produces(MediaType.APPLICATION_JSON)

当前的问题是响应正尝试将我编码到 XML,如错误所指定的那样

MessageBodyWriter not found for media type=application/xml, 
          type=class com.s.b.d.c.CreateBillForStudyCommand

它默认为 XMl,因为您没有在 @Produces 中指定,或者客户端未设置 Accept header 。如果有 Accept: application/json,那么如果没有 @Produces,它将查找 MessageBodyWriter 来处理 JSON。

顺便说一句...

已接受 ( 202 Accepted ) 表示

The request has been accepted for processing, but the processing has not been completed.

您应该使用 created (已创建 201):

The request has been fulfilled and resulted in a new resource being created. The newly created resource can be referenced by the URI(s) returned in the entity of the response, with the most specific URI for the resource given by a Location header field.

使用created,您不需要显式调用location,您传递给该方法的URI将被设置为位置标题。如果要添加body,可以链entity(body)

<小时/>

编辑:

此外,如果您不想在响应中发送表示,您可以简单地调用 no-arg accepted()方法。这不会发送响应正文,并且您不应再收到异常。

关于java - 为什么我在 Jersey REST API 上发布 POST 时收到 500 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30107268/

相关文章:

Java Servlet、JAX-RS - 在基于表单的身份验证工作时遇到问题

java - 如何在 Guice 4.0 中注入(inject)实现

java - 如何在单元测试中处理 session 变量

Java:TableModel 和 TreeModel 的服务器端持久存储解决方案?

spring-boot - 如何在 spring-boot 应用程序中为 swagger-ui 指定我的 restful API

java - jersey web 服务 json utf-8 编码

java - Dropwizard:无法解析配置:logging.appenders

java - 如何在 Dropwizard 中使用 Quartz 调度程序进行 REST 调用?

java - 如何复制剪贴板中的多个文本行并粘贴到另一个非java表单中

java - Android 线程、服务以及它们之间的双向通信