编程语言
首页 > 编程语言> > java – jax-rs泽西@Post响应实体为空

java – jax-rs泽西@Post响应实体为空

作者:互联网

我正在尝试@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&作家.或者Moxy @XmlJavaTypeAdapter.但是因为Lists显然可以像TweetQuery一样开箱即用.

你在外面https://stackoverflow.com/users/383861/blaise-doughan吗?

标签:java,rest,jax-rs,jersey,moxy
来源: https://codeday.me/bug/20190728/1565175.html