How do I fix a NoSuchMethodError?

前端 未结 29 2849
孤独总比滥情好
孤独总比滥情好 2020-11-21 05:08

I\'m getting a NoSuchMethodError error when running my Java program. What\'s wrong and how do I fix it?

29条回答
  •  爱一瞬间的悲伤
    2020-11-21 05:44

    I had the same error:

      Exception in thread "main" java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonGenerator.writeStartObject(Ljava/lang/Object;)V
            at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:151)
            at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:292)
            at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3681)
            at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3057)
    

    To solve it I checked, firstly, Module Dependency Diagram (click in your POM the combination -> Ctrl+Alt+Shift+U or right click in your POM -> Maven -> Show dependencies) to understand where exactly was the conflict between libraries (Intelij IDEA). In my particular case, I had different versions of Jackson dependencies.

    1) So, I added directly in my POM of the project explicitly the highest version - 2.8.7 of these two.

    In properties:

    2.8.7
    

    And as dependency:

    
        com.fasterxml.jackson.core
        jackson-databind
        ${jackson.version}
    
    

    2) But also it can be solved using Dependency Exclusions.

    By the same principle as below in example:

      
          group-a
          artifact-a
          1.0
              
                 
                    com.fasterxml.jackson.core
                    jackson-databind
                 
             
      
    

    Dependency with unwanted version will be excluded from your project.

提交回复
热议问题