与杰克逊一起回应json网络服务

时间:2014-11-01 12:01:58

标签: java json jackson jersey-2.0

嗨我有我的网站服务与泽西岛,我使用杰克逊生成JSON对象,例如我的资源TESTS与set / get方法:

public class Tests {

    private String name;

    private String result;

    private String credit;

}

我的api:

@Path("/getTests")
    @GET
    @ManagedAsync
    @Produces(MediaType.APPLICATION_JSON)
    public void listsTests(@Suspended final AsyncResponse response) {

       ListenableFuture<Collection<Esame>> listTests = service
                .getTestsInterfaceAsync();


        Futures.addCallback(listTests, new FutureCallback<Collection<Tests>>() {

            public void onSuccess(Collection<Tests> tests) {
                response.resume(tests);
            }

            public void onFailure(Throwable thrown) {
                response.resume(thrown);
            }
        });

如果我打电话给我的api工作得很好,我有这个JSON:

{
 name=something
 result=6
 credit=12
}

{
 name=something
 result=6
 credit=12
}

{
 name=something
 result=6
 credit=12
}

{
 name=something
 result=6
 credit=12
}

现在我的问题,如果我想添加响应状态?例如:

{
 status=200
}
{
 name=something
 result=6
 credit=12
}

我必须在Tests类中添加Object状态吗?..但在这种情况下,结果将是:

  {
     status=200
     name=something
     result=6
     credit=12
    }
 {
     status=200
     name=something
     result=6
     credit=12
    }

1 个答案:

答案 0 :(得分:1)

尝试一下

public class Result{
    private Integer status;
    private List<Tests> tests;

    // getters and setters 
}