【问题标题】:response json web services with jackson使用杰克逊响应 json Web 服务
【发布时间】:2014-11-01 12:01:57
【问题描述】:

您好,我有 Jersey 的 Web 服务,我使用 jackson 生成 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
}

我必须在测试类中添加对象状态?..但在这种情况下,结果将是:

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

【问题讨论】:

  • 在你的测试类周围创建一个包装器,比如 StatusTest 并在其中包含状态字段和测试类?

标签: java json jackson jersey-2.0


【解决方案1】:

尝试一下

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

    // getters and setters 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-12
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 2019-05-02
    • 2014-02-12
    相关资源
    最近更新 更多