我正在尝试@POST 一个用户创建的对象并获得一个具有不同用户创建的有效负载作为实体的响应。尽管返回的对象存在并已填充,但在客户端它是空的。
客户端发送/服务器接收对象:
@XmlRootElement
public class TweetQuery {
String query;
List<TweetQueryTweet> tweets = new ArrayList<>();
// setters and getters
}
public class TweetQueryTweet {
private String id;
private String text;
// setters and getters
}
服务器接收/客户端发送对象:
@XmlRootElement
public class TweetClusters {
List<TweetCluster> tweetClusters = new ArrayList<>();
// setters and getters
}
public class TweetCluster {
List<String> labels = new ArrayList<>();
List<String> docs = new ArrayList<>();
// setters and getters
}
客户端(Arquillian)测试:
@Test
@RunAsClient
public void test01SeeSomething(@ArquillianResource URL deploymentUrl) throws ... {
final URI targetURI = ...;
System.out.println(" test target:" + targetURI.toASCIIString());
Entity<TweetQuery> tweetQuery = Entity.entity(getTestTweetQuery(), MediaType.APPLICATION_JSON);
Client client = ClientBuilder.newBuilder().build();
WebTarget target = client.target(targetURI.toASCIIString());
Response response = target.request(MediaType.APPLICATION_JSON).post(tweetQuery);
TweetClusters x = response.readEntity(TweetClusters.class);
System.out.println("Entity:" + x);
System.out.println("Response: " + response.getStatus());
assertEquals(Status.OK.getStatusCode(), response.getStatus());
assertNotNull(x);
assertThat(x.getTweetClusters().size()).isGreaterThan(0);
}
泽西邮政方法:
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response clusterPost(TweetQuery tweetQuery) {
TweetClusters tweetClusters = clusterService.getTweetClusters(tweetQuery);
System.out.println("clusterPost - in - tweetQuery: " + tweetQuery);
System.out.println(" - out tweetClusters: " + tweetClusters);
return Response.status(Status.OK).type(MediaType.APPLICATION_JSON).entity(tweetClusters).build();
}
调试:
test target:http://localhost:18080/test//cluster
clusterPost - in - tweetQuery: [TweetQuery - query:TweetQuery query, tweets:[[TweetQueryTweet - id:1, text:text 1], [TweetQueryTweet - id:2, text:text 2], [TweetQueryTweet - id:3, text:text 3]]]
- out tweetClusters: [TweetClusters:[[TweetCluster - labels: [Other Topics], docs:[3, 2, 1]]]]
Entity:[TweetClusters:[]]
Response: 200
第 2 行 - clusterPost - in - 显示 TweetQuery 正在正确编组。第 3 行 - clusterPost - out - 显示响应实体存在时要发送的 tweetClusters 第 4 行 - tweetClusters 不是从请求中出来的
编辑
我更改了 REST 方法以返回它作为输入接收的 tweetQuery,并且它被正确返回。所以它是关于 TweetClusters 的。也许我需要一个 MessageBodyReader & Writer。或 Moxy @XmlJavaTypeAdapter。但是对于 Lists 显然是开箱即用的,因为 TweetQuery 有效。
https://stackoverflow.com/users/383861/blaise-doughan你在外面吗?:)
我错过了一些简单的东西吗?