我正在尝试@POST用户创建的对象,并获得具有不同用户创建的有效负载作为实体的Response。尽管返回的对象存在并已填充,但在客户端它为空。客户端发送/服务器接收的对象:@XmlRootElementpublic class TweetQuery { String query; List<TweetQueryTweet> tweets = new ArrayList<>(); // setters and getters}public class TweetQueryTweet { private String id; private String text; // setters and getters}服务器接收/客户端发送的对象:@XmlRootElementpublic 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@RunAsClientpublic 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//clusterclusterPost - 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。但是,就像TweetQuery一样,列表显然是开箱即用的。 https://stackoverflow.com/users/383861/blaise-doughan你在外面吗? :)我是否缺少简单的东西? 最佳答案 好吧,我解决了。我无意中告诉了一个猪肉饼。当我说我有二传手和吸气剂时,我是在为TweetClusters误传二传手。然后,一旦修复,我就有了带有参数的构造函数,但没有no-arg构造函数。添加无参数构造函数后,一切都很好。总而言之,您需要将对象编组(取消编组):如果您有arg构造函数,则为无参数构造函数所有元素的设置和获取方法而且,如果您有包括日期和日历在内的更复杂的类型,则需要在Jackson中使用适配器@XmlJavaTypeAdapter(Moxy)或@JsonSerialize.using(或在RESTeasy中使用???)。有趣的是,尽管保留了@XmlRootElement(Moxy),但我不需要将它保留在适当的位置。完整的答案是: 客户端(Arquillian)测试与上面的相同球衣发布方法与上面的相同(un)编组的对象类为:@XmlRootElement公共类TweetClusters { 列出tweetClusters = new ArrayList ();public void addCluster(Cluster c) { TweetCluster tweetCluster = new TweetCluster(c); tweetClusters.add(tweetCluster);}public List<TweetCluster> getTweetClusters() { return tweetClusters;}public void setTweetClusters(List<TweetCluster> tweetClusters) { this.tweetClusters = tweetClusters;}@Overridepublic String toString() { return String.format("[TweetClusters:%s]", tweetClusters);}}公共类TweetCluster { 列表标签= new ArrayList (); 列出文档= new ArrayList ();public TweetCluster() {}public TweetCluster(Cluster c) { labels.add(c.getLabel()); for (Document doc : c.getDocuments()) { docs.add(doc.getTitle()); }}public List<String> getLabels() { return labels;}public void setLabels(List<String> labels) { this.labels = labels;}public List<String> getDocs() { return docs;}public void setDocs(List<String> docs) { this.docs = docs;}@Overridepublic String toString() { return String.format("[TweetCluster - labels: %s, docs:%s]", labels, docs);}}公共类TweetQuery { 字符串查询;List<TweetQueryTweet> tweets = new ArrayList<>();public String getQuery() { return query;}public void setQuery(String query) { this.query = query;}public List<TweetQueryTweet> getTweets() { return tweets;}public void setTweets(List<TweetQueryTweet> tweets) { this.tweets = tweets;}public void addTweets(TweetQueryTweet... queryTweets) { for (TweetQueryTweet tweet : queryTweets) { this.tweets.add(tweet); }}@Overridepublic String toString() { return String.format("[TweetQuery - query:%s, tweets:%s]",query, tweets);}(嗯,对格式感到抱歉;我无法在SO中对其进行修复)出于调试目的,最好取回响应中返回的字符串表示形式(即XML或JSON),而您只需将实体类型指定为String.class即可:String x = response.readEntity(String.class);
10-04 12:14