2015-07-09 37 views
0

我想用我從數據庫中獲取的信息構建一個Response對象。我取回的信息是在更改對象:試圖爲Java REST風格的服務構建Response對象

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對象?

+0

你有錯誤400? – alexvetter

回答

0

嘗試以這種方式交回:

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