简体   繁体   English

Javax REST Response实体= null

[英]Javax REST Response entity = null

I am using javax to create a REST service to send an Java Object from one system to another. 我正在使用javax创建一个REST服务,以便将Java对象从一个系统发送到另一个系统。

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: 我定义了一个应该将实体作为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. 数据以JSON的形式到达,colud被转换回我的java类,我可以再次使用该对象。

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: 屏幕1是我发送之前的回复: 在此输入图像描述

Screen 2 is the response after receiving it: 屏幕2是收到后的响应: 在此输入图像描述

I found a solution. 我找到了解决方案。 I had to add a "valid" readEntity to my WebTarget request. 我必须在我的WebTarget请求中添加一个“有效”的readEntity。 I my case I have written a response object, maybe a String.class might work too. 我的情况我写了一个响应对象,也许String.class也可以。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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