如何解决error“exception in thread”main“java.lang.noclassdeffounderror:com/fasterxml/jackson/annotation/jsonview”

ivqmmu1c  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(1045)

我正在尝试使用jackson库objectmapper计算json字符串。我在pom.xml文件中添加了具有相同版本的jackson注解、jackson databind和jackson core依赖项。但是代码给出了以下错误。我检查了类似的问题,但没有版本不匹配。

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonView
    at com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector.<clinit>(JacksonAnnotationIntrospector.java:37)
    at com.fasterxml.jackson.databind.ObjectMapper.<clinit>(ObjectMapper.java:348)
    at com.company.EvaluateJson.evaluate(EvaluateJson.java:49)
    at com.company.DataMapper.doMapping(DataMapper.java:58)
    at com.company.DataMapper.getRequest(DataMapper.java:49)
    at com.company.Main.main(Main.java:119)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.annotation.JsonView
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 6 more

我的代码如下:

try{
            ObjectMapper objectMapper=new ObjectMapper();

            String jString=objectMapper.writeValueAsString(requestMap);
            JsonNode jsonNode=objectMapper.readTree(jString);
            System.out.println("From jsonNode:"+jsonNode.get("name"));
        }catch (JsonGenerationException e){
            e.printStackTrace();
        }catch (JsonMappingException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }

pom.xml文件

<dependencies>
       <dependency>
           <groupId>com.fasterxml.jackson.core</groupId>
           <artifactId>jackson-core</artifactId>
           <version>2.12.0</version>
       </dependency>
       <dependency>
           <groupId>com.fasterxml.jackson.core</groupId>
           <artifactId>jackson-databind</artifactId>
           <version>2.12.0</version>
       </dependency>
       <dependency>
           <groupId>com.fasterxml.jackson.core</groupId>
           <artifactId>jackson-annotations</artifactId>
           <version>2.12.0</version>
       </dependency>
       <dependency>
           <groupId>com.google.code.gson</groupId>
           <artifactId>gson</artifactId>
           <version>2.3.1</version>
       </dependency>
       <dependency>
           <groupId>com.jayway.jsonpath</groupId>
           <artifactId>json-path</artifactId>
           <version>2.4.0</version>
       </dependency>
   </dependencies>
euoag5mw

euoag5mw1#

你能试着把这个添加到pom.xml吗

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>${jackson.version}</version>
</dependency>

相关问题