我写了一个JAX-RS服务器和客户端都使用泽西岛。我想将我的实体集合发送到客户端,并执行了以下步骤:
我发出了一个请求,客户端在服务器端成功地处理了这个请求,但收到了一个错误:
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/json, type=interface java.util.List, genericType=java.util.List<model.HotelsEntity>.
org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:225)
org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:149)
org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1124)
org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:853)
org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:812)
org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:377)
org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:813)
org.glassfish.jersey.client.JerseyInvocation.access$600(JerseyInvocation.java:90)
org.glassfish.jersey.client.JerseyInvocation$3.call(JerseyInvocation.java:693)
org.glassfish.jersey.internal.Errors.process(Errors.java:315)
org.glassfish.jersey.internal.Errors.process(Errors.java:297)
org.glassfish.jersey.internal.Errors.process(Errors.java:228)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424)
org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:689)
org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:405)
org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:301)
service.HotelService.getHotels(HotelService.java:30)
actions.HotelAction.perform(HotelAction.java:42)
MainServlet.processResponse(MainServlet.java:33)
MainServlet.doPost(MainServlet.java:22)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
服务器:
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getHotelsList(@QueryParam("startDate") String startDate,
@QueryParam("endDate") String endDate) {
List<HotelsEntity> list = hotelService.getAll();
return ResponseFactory.response(Response.Status.OK, list);
}
客户:
GenericType<List<HotelsEntity>> genericType = new GenericType<List<HotelsEntity>>(){};
WebTarget target = client.target(preparePath());
List<HotelsEntity> hotels = target.request(MediaType.APPLICATION_JSON_TYPE).get(genericType);
提供商:
public class JsonProvider<T> implements MessageBodyReader<T>, MessageBodyWriter<T> {
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return MediaType.APPLICATION_JSON.equals(mediaType.getType()) &&
MediaType.APPLICATION_JSON.equals(mediaType.getSubtype());
}
@Override
public T readFrom(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
Gson gson = createGson();
Reader reader = new InputStreamReader(entityStream, Charset.forName(Constants.UTF_8));
return gson.fromJson(reader, genericType);
}
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return MediaType.APPLICATION_JSON.equals(mediaType.getType()) &&
MediaType.APPLICATION_JSON.equals(mediaType.getSubtype());
}
@Override
public long getSize(T t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return -1;
}
@Override
public void writeTo(T t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
Gson gson = createGson();
JsonElement element = gson.toJsonTree(entityStream);
Writer writer = null;
try {
writer = new OutputStreamWriter(entityStream, Charset.forName(Constants.UTF_8));
gson.toJson(element, writer);
} finally {
if (writer != null) {
writer.flush();
}
}
}
private Gson createGson() {
return new GsonBuilder().setPrettyPrinting().create();
}
}
@Provider
public class JsonCollection extends JsonProvider<Collection<? extends HospitalityEntity>> {}
@Entity
@Table(name = "hotels", schema = "", catalog = "mydb")
public class HotelsEntity implements HospitalityEntity{
private int idHotel;
private String name;
private String region;
private String description;
@Id
@Column(name = "id_hotel")
public int getIdHotel() {
return idHotel;
}
public void setIdHotel(int idHotel) {
this.idHotel = idHotel;
}
@Basic
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "region")
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
@Basic
@Column(name = "description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
HotelsEntity that = (HotelsEntity) o;
if (idHotel != that.idHotel) return false;
if (description != null ? !description.equals(that.description) : that.description != null) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
if (region != null ? !region.equals(that.region) : that.region != null) return false;
return true;
}
@Override
public int hashCode() {
int result = idHotel;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (region != null ? region.hashCode() : 0);
result = 31 * result + (description != null ? description.hashCode() : 0);
return result;
}
}
您的实体类没有空的构造函数,这是JAX-RS解组所需要的。
在此处查看:
https://blogs.oracle.com/groundside/entry/jax_rs_2_0_messagebodyreader
很抱歉复活这篇文章,但我在Maven项目中遇到了这个问题,发现我需要在我的pom中包含杰克逊 -jaxrs-json-提供者
的依赖关系:
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.4.1</version>
</dependency>
MVN 存储库:http://mvnrepository.com/artifact/com.fasterxml.jackson.jaxrs/jackson-jaxrs-json-provider
您可以使用jersey json库:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.22</version>
</dependency>
或玄关:
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>1.3</version>
</dependency>
你知道吗? 谢谢,米凯尔
消息:找不到类型为java的响应对象的MessageBodyWriter。util。媒体类型的数组列表:application/json 描述:服务器遇到内部错误(找不到类型为java.util.ArrayList,媒体类型为application/json的响应对象的MessageBodyWriter),导致其无法满足此请求 我在类路径中找到了resteasy jackson提供程序。尝试将返回
我正在使用jersey客户端调用rest webservice。 我的网络服务正在使用json,所以我需要让json打电话给我的网络服务提供商。 我用下面的方法做这件事。 但是我得到了以下异常: 09:52:01,625错误[[MVC-dispatcher]]servlet MVC-dispatcher的Servlet.service()抛出异常com . sun . jersey . API .
问题内容: 我正在尝试简单的Jersey + JSON示例,但出现以下错误 我放入以下jar文件以获得适当的结果 为什么会出现此类错误?错误日志在这里: web.xml JsonExample.java 和Json服务 如果我做错了事,请提出建议。 问题答案: 此问题已通过jersey-bundle-1.8.jar修复
我在使用JAX-RS服务作为JSON时遇到了一些问题。
在编写一个使用XML数据的简单Jersey客户端时,我遇到了一个异常“MessageBodyReader not found for media type=application/XML”。我的所有设置,包括作为maven依赖项的jersey客户端都很好。我使用的版本是2.17。一旦我将版本降级到2.15,它就可以正常工作了。谁能解释一下版本2.17需要包含哪些依赖项才能工作吗。 Maven依赖项