0

I'm trying to implement a small JAX-RS based REST application. It has a backend server and a small client. The client basically reads from a Table and sends the table data as POST values to the REST server. This is the resource method that handles POST request.

@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();
}

This is the client code:

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() is supposed to read the table and store the value in a List<>. Then the list is iterated inside a for loop, values are extracted and assigned to the instances of Attachment class and finally sent as POST. Now, when I run the client, it works until System.out.println(attachment.getCustomerId()+" "+attachment.getSize());

The output enter image description here

After that the client gives a HTTP 500 request failed error like this

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.}}

When I try to manually enter data via a main method in the backend like this, it works fine.

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();
    }


}

I've been stuck with this for a while now, I'd really appreciate any help/guidance.

Roshan Upreti
  • 1,782
  • 3
  • 21
  • 34
  • 1
    500 is an internal Server error. Look into Server logfiles for more informations – Jens Sep 10 '18 at 07:18
  • @Jens, I'm using grizzly server and I don't seem to find any logfiles. :( – Roshan Upreti Sep 10 '18 at 07:24
  • Have you tried with another dataset? Maybe the issue is with the data encountered. Try some other data set once. – Tanu Garg Sep 10 '18 at 07:27
  • @TanuGarg, It's basically two Integers that need sending. When I pass them manually using a main method in the backend side it works fine, just as expected. – Roshan Upreti Sep 10 '18 at 07:30
  • read maybe it helps you finding the logfiles: https://stackoverflow.com/questions/21329733/grizzly-standalone-logging/28971975 – Jens Sep 10 '18 at 07:35

0 Answers0