小编Cob*_*ard的帖子

Java使用Rest服务GET中的JSON列表

我收到的错误:

SEVERE: A message body reader for Java class java.util.List, 
and Java type java.util.List<com.testapp.Category>, 
and MIME media type text/html; charset=utf-8 was not found
Run Code Online (Sandbox Code Playgroud)

尝试使用带有Jersey的GET方法从Rest服务使用JSON响应.当我使用curl时,服务器的响应如下所示:

[{"category":{"id":"4d9c5dfc8ddfd90828000002","description":"Cows"}},
{"category":{"id":"4d9c5dfc8ddfd90828000023","description":"Dogs"}},
...
{"category":{"id":"4d9c5dfc8ddfd90828000024","description":"Mules"}}]
Run Code Online (Sandbox Code Playgroud)

使用服务:

public List<Category> getAnimalCategories(Cookie cookie) {
    Client client = Client.create(new DefaultClientConfig());
    ClientResponse response = client
        .resource(Constants.BASE_URL)
        .path(Constants.CATEGORIES_ANIMALS)
        .accept(MediaType.APPLICATION_JSON)
        .type(MediaType.APPLICATION_JSON)
        .cookie(cookie)
        .get(ClientResponse.class);

    return response.getEntity(new GenericType<List<Category>>(){});
}
Run Code Online (Sandbox Code Playgroud)

Category.java的位置是:

public class Category {

public String id;
public String description;

public Category() {
}

public Category(String id, String description) {
    super();
    this.id = id;
    this.description = …
Run Code Online (Sandbox Code Playgroud)

java rest json arraylist jersey

1
推荐指数
1
解决办法
7868
查看次数

标签 统计

arraylist ×1

java ×1

jersey ×1

json ×1

rest ×1