Garret Wilson
Garret Wilson

Reputation: 21326

swagger-maven-plugin triggers Javadoc warning: element value must be a constant expression (but it is!) in Java annotation

Javadoc (via Maven) is giving me the following error in one my Java JAX-RS interface method signatures:

error: element value must be a constant expression

Here is my JAX-RS interface:

public interface FooResource {

  @Consumes(APPLICATION_FORM_URLENCODED_UTF_8)
  public void bar();

}

Javdoc gives the error for @Consumes. Here is the definition for APPLICATION_FORM_URLENCODED_UTF_8, which appears in MyAppConstants in the same project:

public static final String APPLICATION_FORM_URLENCODED_UTF_8 =
    APPLICATION_FORM_URLENCODED + ";" + CHARSET_PARAMETER + "=UTF-8";

And here is the definition of APPLICATION_FORM_URLENCODED, which appears in javax.ws.rs.core.MediaType:

public final static String APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded";

And here is the definition of CHARSET_PARAMETER, which also appears in javax.ws.rs.core.MediaType:

public static final String CHARSET_PARAMETER = "charset";

Now I ask you: what about APPLICATION_FORM_URLENCODED_UTF_8 is not constant at compile time?

The error message didn't say that I have to provide a literal. It said I had to provide a constant. So what about this is not a constant?

(I could almost swear that this worked at one time but suddenly stopped working.)

Update: Found cause, but still don't understand.

For some reason, merely including the swagger-maven-plugin in the POM will trigger this problem! My code doesn't change at all, but as soon as I add the following dependency, suddenly I get Javadoc warnings for my existing code!!!

<dependency>
  <groupId>com.github.kongchen</groupId>
  <artifactId>swagger-maven-plugin</artifactId>
  <version>3.1.5</version>
</dependency>

How can a single dependency make Javadoc work differently on a code file? What is swagger-maven-plugin doing?

Upvotes: 7

Views: 480

Answers (1)

Garret Wilson
Garret Wilson

Reputation: 21326

My best guess is that this happens because swagger-maven-plugin transitively (via io.swagger:swagger-core:1.5.13) an old version of the JAX-RS specification in javax.ws.rs:jsr311-api:1.1.1. Note that the JAX-RS 2 artifact ID is javax.ws.rs-api, Maven doesn't realize that they are different versions of the same JAR, and pulls them both in as dependencies. I can only guess that javax.ws.rs:jsr311-api in fact does not use constants for the variables in question. In any case, when I threw out swagger-maven-plugin and pulled in io.swagger:swagger-annotations (which was all I needed in this project for documentation), the problem went away.

See https://github.com/kongchen/swagger-maven-plugin/issues/543.

Upvotes: 0

Related Questions