我对RESTful Web服务有以下get请求:
List<ShoppingEntry> entries = client.target("http://localhost:8080/MMServer/webresources/shopping/getEntries")
.request(MediaType.APPLICATION_JSON)
.get()
.readEntity(new GenericType<List<ShoppingEntry>>() {});
和相应的资源
@GET
@Path("getEntries")
@Produces(MediaType.APPLICATION_JSON)
public List<ShoppingEntry> getShoppingBundle() {
List<ShoppingEntry> shoppingEntries = new ArrayList<>();
List<Shopping> shoppingList = super.findAll();
ShoppingEntry entry;
for (Shopping s: shoppingList) {
entry = new ShoppingEntry();
entry.setName(s.getIdperson().getFirstname());
entry.setDate(s.getDate());
entry.setAmount(s.getAmount());
entry.setStore(s.getStore());
shoppingEntries.add(entry);
}
return shoppingEntries;
}
问题:有时程序运行没有错误,一切都没问题。但大多数时候我得到以下例外:
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:找不到媒体类型= text / html的MessageBodyReader,type = interface java.util.List,genericType = java.util.List。
其中说:“type = text / html”应该是什么原因。怎么会发生这种情况? 我的依赖是:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.26</version>
</dependency>
</dependencies>