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

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

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

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

我的代码如下:

  1. try{
  2. ObjectMapper objectMapper=new ObjectMapper();
  3. String jString=objectMapper.writeValueAsString(requestMap);
  4. JsonNode jsonNode=objectMapper.readTree(jString);
  5. System.out.println("From jsonNode:"+jsonNode.get("name"));
  6. }catch (JsonGenerationException e){
  7. e.printStackTrace();
  8. }catch (JsonMappingException e){
  9. e.printStackTrace();
  10. }catch (IOException e){
  11. e.printStackTrace();
  12. }

pom.xml文件

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.fasterxml.jackson.core</groupId>
  4. <artifactId>jackson-core</artifactId>
  5. <version>2.12.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.fasterxml.jackson.core</groupId>
  9. <artifactId>jackson-databind</artifactId>
  10. <version>2.12.0</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>com.fasterxml.jackson.core</groupId>
  14. <artifactId>jackson-annotations</artifactId>
  15. <version>2.12.0</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>com.google.code.gson</groupId>
  19. <artifactId>gson</artifactId>
  20. <version>2.3.1</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>com.jayway.jsonpath</groupId>
  24. <artifactId>json-path</artifactId>
  25. <version>2.4.0</version>
  26. </dependency>
  27. </dependencies>
euoag5mw

euoag5mw1#

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

  1. <dependency>
  2. <groupId>com.fasterxml.jackson.core</groupId>
  3. <artifactId>jackson-annotations</artifactId>
  4. <version>${jackson.version}</version>
  5. </dependency>

相关问题