Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring nullable annotation generates unknown enum constant warning

In my app, whenever I add @Nullable (which imports from org.springframework.lang.Nullable) to any of the fields, I get a build warning:

Warning:java: unknown enum constant javax.annotation.meta.When.MAYBE reason: class file for javax.annotation.meta.When not found

@NonNull and other null safety annoations from spring compile without any warnings as its implementation doesn't import import javax.annotation.meta.When.

The application runs just fine but the warning is just annoying. I am using spring boot 2.1.0 and java version 1.8.0_191

like image 547
夢のの夢 Avatar asked Nov 15 '18 19:11

夢のの夢


2 Answers

This warning is caused by the javax.annotation.meta.When enum not being available to your projects runtime (org.springframework.lang.Nullable references this enum but it is not made available automatically). You need to bring in a JSR305 implementation to fix this warning.

the Google find bugs repo includes a JSR305 implementation that should fix the problem: https://mvnrepository.com/artifact/com.google.code.findbugs/jsr305

since you are using gradle, add the dependency to your build.gradle script:

...
dependencies {
    ...

     // https://mvnrepository.com/artifact/com.google.code.findbugs/jsr305
    implementation 'com.google.code.findbugs:jsr305:3.0.2'

    ...
}
...

do a clean build and the error should go away


If you do not want to use the com.google.code.findbugs group's artifact you can try another from this list: https://mvnrepository.com/search?q=JSR305

references:

  • Strange Eclipse IDE error javax.annotation.meta.When #Java
  • What happened to Java's @OverridingMethodMustCallSuper?
  • https://github.com/sbrannen/runtime-annotations/blob/master/src/main/java/org/example/SpringApplication.java
like image 62
riiich Avatar answered Nov 16 '22 06:11

riiich


it's bothered me too. just try this in your pom:

    <dependency>
        <groupId>com.google.code.findbugs</groupId>
        <artifactId>annotations</artifactId>
        <version>3.0.1</version>
    </dependency>

it's work for me.

like image 11
Kelvin Wong Avatar answered Nov 16 '22 06:11

Kelvin Wong