JAX-RS:如何将我的对象列表作为JSON返回? [英] JAX-RS: How can I return my list of objects as JSON?

查看:472
本文介绍了JAX-RS:如何将我的对象列表作为JSON返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了杰克逊的文档,这让我感到困惑:(
我的实体看起来像:

I looked at the documentation of Jackson, and it left me confused :( My entity looks like :

 @Entity
 @Table(name = "variable")
 public class Variable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(unique = true, nullable = false)
    private String name;

    @Column
    @Enumerated(EnumType.STRING)
    private VariableType type;

    @Column(nullable = false)
    private String units;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_on", nullable = false)
    private Date createdOn;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "retired_on", nullable = true)
    private Date retiredOn;

    @Column(nullable = false)
    private boolean core;

}

和我的 JAX-RS 服务看起来像

@Path("/variable")
public class VariableResource {
    @Inject private VariableManager variableManager;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getVariables() {
        return Response.ok(variableManager.getVariables()).build();
    }

}

当我使用<测试此服务时code> curl http:// localhost:8080 / app / rest / variable ,我在服务器日志中看到以下内容

When I test this service using curl http://localhost:8080/app/rest/variable, I see the following in my server logs

[javax.ws.rs.core.Application]] (http--127.0.0.1-8080-6) Servlet.service() for servlet javax.ws.rs.core.Application threw exception: java.lang.NoSuchMethodError: org.codehaus.jackson.type.JavaType.<init>(Ljava/lang/Class;)V

我可以将变量列表作为JSON返回的最简单方法是什么?

What are some simplest ways I can return my list of variables as JSON?

推荐答案

通常它就像在你的实体上添加 @XmlRootElement 一样简单(我可以看到你正在使用JPA / Hibernate @Entity / @Table ,但你错过了 @XmlRootElement )。

Normally it is as simple as adding the @XmlRootElement on your Entity (I can see you're using JPA/Hibernate @Entity/@Table, but you're missing the @XmlRootElement).

@Entity
@Table(name = "variable")
@XmlRootElement
public class Variable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(unique = true, nullable = false)
    private String name;

    // ...

    @Column(nullable = false)
    private boolean core;
}

这是针对服务的,使用回复来自JAX-RS的,并且还直接返回将由JAX-RS自动封送的对象:

And this is for the service, using the Response from JAX-RS, and also returning directly an object that will be marshaled automatically by JAX-RS:

@Path("/variable")
public class VariableResource {
    @Inject private VariableManager variableManager;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getVariables() {
        return Response.ok(variableManager.getVariables()).build();
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    // Same method but without using the JAX-RS Response object
    public List<Variable> getVariablesAlso() {
        return variableManager.getVariables();
    }
}

通常人们会创建 DTO ,以避免将实体的内部值从数据库暴露给现实世界,但如果它没有问题,则不是强制性的让你揭露整个对象。

Often people would create a DTO to avoid exposing internal values of the Entity from the database to the real world, but it is not mandatory, if it is fine for you to expose the whole object.

这篇关于JAX-RS:如何将我的对象列表作为JSON返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆