无法在 java 中创建 objectMapper



我正在创建对象映射器的实例。

ObjectMapper objectMapper = new ObjectMapper();

这一行给了我以下错误:

构造函数 ObjectMapper(( 未定义

我做错了什么?你能帮帮我吗?提前谢谢。

如果你使用 fasterxml jackson ObjectMapper,那么有一个空的构造函数:

/*
/**********************************************************
/* Life-cycle: constructing instance
/**********************************************************
*/
/**
* Default constructor, which will construct the default
* {@link JsonFactory} as necessary, use
* {@link SerializerProvider} as its
* {@link SerializerProvider}, and
* {@link BeanSerializerFactory} as its
* {@link SerializerFactory}.
* This means that it
* can serialize all standard JDK types, as well as regular
* Java Beans (based on method names and Jackson-specific annotations),
* but does not support JAXB annotations.
*/
public ObjectMapper() {
this(null, null, null);
}

这是从JavaDoc复制的。请确保导入语句正确无误。我正在使用

import com.fasterxml.jackson.databind.ObjectMapper;

它对我有用。

检查类中的库。

如果您错误地导入了具有相同名称的其他库,则会引起问题。

import org.elasticsearch.index.mapper.ObjectMapper;
import com.fasterxml.jackson.databind.*;
ObjectMapper objectMapper = new ObjectMapper();

在上面的示例中,应删除第一行。

如果您使用的是杰克逊,您可能会缺少以下 jar

jackson-mapper-asl-1.5.0.jar

也尝试导入下面的包

import com.fasterxml.jackson.databind.*;

最新更新