[英]Jersey - return list of XmlRootElement/Entities - MessageBodyWriter not found
There are a million of these JAX-RS serialization snowflakes. 这些JAX-RS序列化雪花有百万。 Let me add one more.
让我再添加一个。
I have a JPA/JAXb annotated class: 我有一个JPA / JAXb注释类:
@Entity
@XmlRootElement
public class Foo implements Serializable {
//..
}
I have a service that seeks to return a list of these objects as JSON: 我有一个旨在将这些对象的列表作为JSON返回的服务:
@Path("foo")
public class FooService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response listFoos() {
// get hibernate session
List<Foo> foos = session.createQuery("from Foo").list();
GenericEntity<List<Foo>> entity = new GenericEntity<List<Foo>>(foos) {};
return Response.ok(entity).build();
}
}
I have tried a number of variations. 我尝试了多种变体。 The one that looks the best, to me is like
对我来说看起来最好的那个就像
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Foo> listFoos() {
// get session ..
return session.createQuery("from Foo").list();
}
All give me some slight variation of: 都给我一些细微的变化:
SEVERE: MessageBodyWriter not found for
media type=application/json, type=class java.util.ArrayList,
genericType=java.util.List<com.bar.baz.Foo>.
Don't forget to add a dependency like: 不要忘了添加类似的依赖项:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.