java JAX-RS:如何将对象列表作为 JSON 返回?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14326011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAX-RS: How can I return my list of objects as JSON?
提问by daydreamer
I looked at the documentation of Hymanson, and it left me confused :( My entity looks like :
我查看了Hymanson的文档,这让我感到困惑:( 我的实体看起来像:
@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;
}
and my JAX-RS
service looks like
我的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();
}
}
When I test this service using curl http://localhost:8080/app/rest/variable
, I see the following in my server logs
当我使用 测试此服务时curl http://localhost:8080/app/rest/variable
,我在服务器日志中看到以下内容
[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.Hymanson.type.JavaType.<init>(Ljava/lang/Class;)V
What are some simplest ways I can return my list of variables as JSON?
有哪些最简单的方法可以将我的变量列表作为 JSON 返回?
回答by Alex
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
).
通常它就像@XmlRootElement
在您的实体上添加 一样简单(我可以看到您正在使用 JPA/Hibernate @Entity
/ @Table
,但您错过了@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;
}
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:
这是针对服务的,使用Response
from JAX-RS,并直接返回一个将由 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();
}
}
Often people would create a DTOto 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.
通常人们会创建一个DTO来避免将 Entity 的内部值从数据库暴露给现实世界,但这不是强制性的,如果你可以公开整个对象。