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