7

Spring documentation tells that, if we compile our project using Java 8 --parameters flag, we can skip giving parameter names in annotations like @PathVariable. That means, we can just use @PathVariable id instead of @PathVariable("id") id.

In a Spring Boot Maven application, I was curious to know how to tell the compiler to use the parameters flag. Is it on by default? Do we need to provide something in the pom.xml?

Sanjay
  • 8,755
  • 7
  • 46
  • 62

2 Answers2

12

In Spring Boot 2.0, the --parameters flag should be enabled by default. See yuranos87's answer.

For older versions, in the pom.xml file, you can specify Java compiler options as arguments of the Maven compiler plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <compilerArgs>
            <arg>-parameters</arg>
        </compilerArgs>
    </configuration>
</plugin>
approxiblue
  • 6,982
  • 16
  • 51
  • 59
12

I don't remember needing to do it explicitly in any of my projects. Maybe you just need to add spring-boot-starter-parent(I know, sometimes might not be an option). Otherwise, Spring has already taken care of everything for you.

It is mentioned multiple times in Spring Boot documentation. For example, here:

To allow the input to be mapped to the operation method’s parameters, code implementing an endpoint should be compiled with -parameters. This will happen automatically if you are using Spring Boot’s Gradle plugin or if you are using Maven and spring-boot-starter-parent.

UPDATE

The way Spring Boot does it is quite straight forward(in spring-boot-parent and spring-boot-starter-parent poms):

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <parameters>true</parameters>
            </configuration>
        </plugin>
approxiblue
  • 6,982
  • 16
  • 51
  • 59
yuranos
  • 8,799
  • 9
  • 56
  • 65
  • 1
    Oh nice. Any idea this is added in Boot 2.0 or was present earlier? In a Boot 1.4 Gradle project, it's not working for me. – Sanjay Mar 17 '18 at 12:32
  • I will check maven Spring BOM, because I'm sure it's there for a while and we rely on this for ages, but it seems for gradle it has been indeed added quite recently: https://github.com/spring-projects/spring-boot/blob/29c3be3590c448476f8b1e8168cf3a1bfb98e67e/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/JavaPluginAction.java#L49. This line is coming from commit: "Use -parameters compiler arg by default in Gradle builds" of 9/20/2017 – yuranos Mar 17 '18 at 17:45
  • @Sanjay, Imagine my surprise. For maven, the situation is similar. It was added quite recently: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-parent/pom.xml#L599 "Enable "-parameters" compiler flag by default with Maven" of 7/31/2017. Now I need to figure out how we configured it in the past:) – yuranos Mar 17 '18 at 18:03
  • Nice find! For reference, here are the pull requests for [Maven/Kotlin builds](https://github.com/spring-projects/spring-boot/pull/12641) and for [Gradle builds](https://github.com/spring-projects/spring-boot/pull/9839). – approxiblue Sep 29 '18 at 23:38