gpt4 book ai didi

Java - Jersey 返回字符串列表

转载 作者:行者123 更新时间:2023-11-30 07:16:27 24 4
gpt4 key购买 nike

我正在尝试根据数据库(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>.

提前谢谢大家。

最佳答案

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

关于Java - Jersey 返回字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38309932/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com