Jackson无法反序列化通过REST传递的MongoDB对象 [英] Jackson can't deserialize MongoDB object passed through REST
问题描述
尝试设置应用程序以在Web应用程序中显示MongoDB嵌套数据(嵌套和嵌入的两个级别)(用于业务分析)。
Trying to set up an application to display MongoDB nested data (two levels of nesting and embedding) in a web application (for business analysis).
我是使用Dropwizard,所以我复制了 dropwizard-mongo 示例,将其减少到必要的部分(没有删除,没有插入,没有指标或任何东西)。
I'm using Dropwizard, so I copied the dropwizard-mongo example, reduced it to the necessary parts (no delete, no insert, no metrics or anything).
App.java
package test;
import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import com.meltmedia.dropwizard.mongo.MongoBundle;
import test.Res;
public class App extends Application<Config> {
public static void main(String[] args) throws Exception {
new App().run(args);
}
MongoBundle<Config> mongoBundle;
@Override
public void initialize(Bootstrap<Config> bootstrap) {
bootstrap.addBundle(mongoBundle =
MongoBundle.<Config> builder()
.withConfiguration(Config::getMongo).build());
}
@Override
public void run(Config config, Environment env) throws Exception {
env.jersey().register(new Res(mongoBundle.getDB()));
}
}
Res.java (资源类)
Res.java (Resource class)
package test;
import java.util.List;
import java.util.Set;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.jongo.Jongo;
import org.jongo.MongoCollection;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mongodb.DB;
@Path("/")
public class Res {
DB database;
Jongo jongo;
public Res(DB database) {
this.database = database;
this.jongo = new Jongo(database);
}
@GET
@Produces("application/json")
public Set<String> collectionNames() {
return database.getCollectionNames();
}
@Path("{collectionName}")
public CollectionResource collection(@PathParam("collectionName") String name) {
return new CollectionResource(jongo.getCollection(name));
}
public class CollectionResource {
MongoCollection collection;
public CollectionResource(MongoCollection collection) {
this.collection = collection;
}
@GET
@Produces("application/json")
public List<String> list() {
return collection.distinct("_id").as(String.class);
}
@GET
@Path("{id}")
@Produces("application/json")
public ObjectNode getDocument(@PathParam("id") String id) {
ObjectNode node = collection.findOne("{_id: #}", id).as(ObjectNode.class);
if (node == null) {
throw new WebApplicationException(Response.status(Status.NOT_FOUND).build());
}
return node;
}
}
}
我可以卷曲到/并将返回集合,所以我会说与数据库的连接有效。但是,如果我卷曲任何集合,无论是实际数据还是只有普通KVP字母和数字的虚拟对象,甚至只是一个空对象(仅限_id),我总是得到
I can curl to / and will get the collections returned, so I'll say the connection to the DB works. However, if I curl any of the collections, be it the actual data or a dummy with just plain KVP of letters and numbers or even just an empty object (_id only), I always get
com.fasterxml.jackson.databind.JsonMappingException:org.bson.types.ObjectId无法强制转换为java.lang.String
com.fasterxml.jackson.databind.JsonMappingException: org.bson.types.ObjectId cannot be cast to java.lang.String
鉴于我对这个例子几乎没有做任何事情,我有点想知道如何处理这个问题。
Given I hardly did anything to the example, I'm somewhat at loss for ideas what to do about this.
推荐答案
你有一个 POST
方法创建数据还是你正在使用现有的集合?
Do you have a POST
method creating data or are you using an existing collection?
你可以看到这里在他们的例子中,他们是明确地处理 _id
作为字符串
而不是 ObjectId
。我的猜测就是绕过你在这里看到的问题。
You can see here that in their example, they are are explicitly treating _id
as a String
and NOT an ObjectId
. My guess is that was to get around the very issue you are seeing here.
Jongo有一个映射 ObjectId
的许多方法,但它们都需要使用自定义POJO并注释 id
字段,因此它们不会像显示的示例那样灵活。
Jongo has many ways of mapping an ObjectId
, but they all requiring using a custom POJO and annotating the id
field so they wont be as flexible as the example shown.
如果可能,转换所有 _id
数据库中的字段是纯字符串,否则你需要找到一些处理 ObjectId
类型(输入到POJO或修改Jongo Mapper 或许)。
If possible, convert all your _id
fields in the database to be plain strings, otherwise you will need to find someway to deal with the ObjectId
type (either typing to to a POJO or modifying the Jongo Mapper perhaps).
祝你好运:)
这篇关于Jackson无法反序列化通过REST传递的MongoDB对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!