在基于JAX-RS的REST应用程序中发送POST请求时,HTTP 500请求失败

时间:2018-09-10 07:17:28

标签: java rest post jax-rs

我正在尝试实现一个基于小型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());

输出 enter image description here

此后,客户端会发出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();
    }


}

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

0 个答案:

没有答案