尝试为Java RESTful服务构建Response对象

时间:2015-07-09 20:41:57

标签: java rest java-ee

我正在尝试使用我从数据库中获取的信息构建一个Response对象。我收到的信息在一个Change对象中:

change.getDocument()回来是:

{"testSolution":false,"solutionId":333,"clientId":4018593,"firewall":false,"pod":"pod3.dmy3","networkPolicy":{"speed":"1","subnets":{"nat":{"vlan":3004,"cidr":["110.168.0.0/24"]},"private":{"vlan":3004,"cidr":["15.10.1.128/25"]}}}}

所以我正在创建一个这样的响应:

 return Response.status(422).location(location).entity(changeRequest).build();

在单元测试中,我这样做:

    String responseJson = response.readEntity(String.class);
    System.out.println("The response is: " + responseJson);

进行测试,但我得到了这个结果:

The response is: {"object_id" : 333,"object_type" : "class com.entities.Solution", "operation" : "", "revision" : 0, "remote_user" : "",   "remote_host" : "", "created" : "", "published" : "", "comment" : "HTTP Status Code 422", "error" : "HTTP Status Code 422, there is a pending change request for this Solution. Replying with pending ChangeRequest",  "document" : {} }

所有内容都符合预期,但文档节点不包含任何信息。我是否正确构建了Response对象?

1 个答案:

答案 0 :(得分:0)

尝试以这种方式返回:

Response.status(Status.OK).type(MediaType.APPLICATION_JSON)
                .entity(changeRequest).build();