简体   繁体   中英

Javax REST Response entity = null

I am using javax to create a REST service to send an Java Object from one system to another.

I send the data like follows:

WebTarget wt = client.target(baseUrl.toString()).path(restUrlSuffix);
response = wt.request(MediaType.APPLICATION_JSON).post(Entity.json(transferJSON));

I defined a method which should receive the entity as a JSON:

@POST
@Path("/post")
@Consumes("application/json")
@Produces("application/json")
public Response saveWorkflowDefinition(@Valid String json) {
    .....
    .....
    String message = "Message to return";
    Response res = Response.ok(message).build();
    return res;
}

With this method everything is fine. Data arrives as JSON, colud be transformed back to my java class and I can work with the object again.

Also it seems, that the Response is correct. If I debug my code, the response is properly filled.

But on the side where I want to receive this response and check it, the entity part is empty.

I have no idea why?

Screen 1 is my response before sending it: 在此输入图像描述

Screen 2 is the response after receiving it: 在此输入图像描述

I found a solution. I had to add a "valid" readEntity to my WebTarget request. I my case I have written a response object, maybe a String.class might work too. I need my response class later in my code to transfer some more detailed information.

response = wt.request(MediaType.APPLICATION_JSON).post(Entity.json(transferJSON)).readEntity(WFResponse.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM