【问题标题】:Java - Jersey returning a list of StringsJava - Jersey 返回字符串列表
【发布时间】:2016-07-12 16:26:08
【问题描述】:

我正在尝试根据我的数据库 (MongoDB) 中的查询返回字符串列表。 我已经看过Jersey: Return a list of strings,但它对我没有用。

这里是查询代码:

public List<String> getAllContatos() {
    // TODO Auto-generated method stub
    List<String> contatos = new ArrayList<>();
    MongoDatabase db = DaoFactory.getInstance().getMongoDatabase();
    MongoCollection<Document> table = db.getCollection("Contatos");

    for (Document doc : table.find())
        contatos.add(doc.toJson());

    return contatos;
}

这是 REST 代码:

@GET
@Path("/all")
@Produces(MediaType.APPLICATION_JSON)
public Response getAllContacts() {
    operations = new ContatoDaoImpl();
    List<String> documents = operations.getAllContatos();
    GenericEntity<List<String>> contacts = new GenericEntity<List<String>>(documents) {

    };

    return Response.ok(contacts).build();
}

但它仍在返回

GRAVE: MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=java.util.List<java.lang.String>.

提前谢谢大家。

【问题讨论】:

    标签: java mongodb rest jersey


    【解决方案1】:

    我能够解决更改我的 pom.xml 依赖项并更改我的方法 getAllContatos() 及其返回类型的问题。

    【讨论】:

      猜你喜欢
      • 2011-07-20
      • 2012-09-10
      • 2018-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多