首页 > 解决方案 > 在基于 JAX-RS 的 REST 应用程序中发送 POST 请求时 HTTP 500 请求失败

问题描述

我正在尝试实现一个基于 JAX-RS 的小型 REST 应用程序。它有一个后端服务器和一个小客户端。客户端基本上从表中读取数据并将表数据作为 POST 值发送到 REST 服务器。这是处理 POST 请求的资源方法。

@POST
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public Response migrateToMinio(Attachment attachment, @Context UriInfo uriInfo) throws Exception {
    Integer id = attachmentService.createNew(attachment);
    UriBuilder builder = uriInfo.getAbsolutePathBuilder();
    builder.path(Integer.toString(id));
    return Response.created(builder.build()).build();
}

这是客户端代码:

public static void main(String[] args) throws Exception {
    // Fire first (full?) update trigger here
    fireInitialMigration();
    // For subsequent (incremental?) updates, schedule an HTTP POST to occur at a fixed rate:
    EXECUTOR.scheduleAtFixedRate(() -> fireSubsequentUpdate(), INITIAL_DELAY, UPDATE_RATE, TimeUnit.MILLISECONDS);
    // Keep main thread alive
    while (true) ;
}

private static void fireInitialMigration() throws Exception {
    TiedostoService tiedostoService = new TiedostoService();
    List<Tiedosto> tiedostoList = tiedostoService.getAllFiles();
    Client client = ClientBuilder.newClient();
    List<Response> responseList = new ArrayList<>();
    for (Tiedosto tiedosto : tiedostoList){
        Attachment attachment = new Attachment();
        attachment.setCustomerId(tiedosto.getCustomerId());
        attachment.setSize(tiedosto.getFileSize());
        System.out.println(attachment.getCustomerId()+" "+attachment.getSize());
        Response res = client.target("http://localhost:8080/myapp/attachments")
            .request("application/json")
            .post(Entity.entity(attachment, MediaType.APPLICATION_JSON), Response.class);
        responseList.add(res);
    }
    System.out.println(responseList);
}

private static void fireSubsequentUpdate() {
    // Similar to initialMigration(), but change Entity/set of HTTP POSTs according to your needs.
}

fireInitialMigration() 应该读取表并将值存储在 List<> 中。然后在 for 循环中迭代列表,提取值并将其分配给 Attachment 类的实例,最后作为 POST 发送。现在,当我运行客户端时,它一直工作到System.out.println(attachment.getCustomerId()+" "+attachment.getSize());

输出 在此处输入图像描述

之后,客户端给出这样的 HTTP 500 请求失败错误

InboundJaxrsResponse{context=ClientResponse{method=POST, uri=http://localhost:8080/myapp/attachments, status=500, reason=Request failed.}}, InboundJaxrsResponse{context=ClientResponse{method=POST, uri=http://localhost:8080/myapp/attachments, status=500, reason=Request failed.}}, InboundJaxrsResponse{context=ClientResponse{method=POST, uri=http://localhost:8080/myapp/attachments, status=500, reason=Request failed.}}

当我尝试通过这样的后端的主要方法手动输入数据时,它工作正常。

public static void main(String[] args) {
    AttachmentService attachmentService = new AttachmentService();
    Integer id = null;
    Attachment attachment = new Attachment();
    attachment.setCustomerId(150);
    attachment.setSize(8096);

    try{

        id = attachmentService.createNew(attachment);

    } catch (Exception e) {
        e.printStackTrace();
    }


}

我已经坚持了一段时间了,我非常感谢任何帮助/指导。

标签: javarestpostjax-rs

解决方案


推荐阅读