Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile Spring Boot applications with Java 8 --parameter flag

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?

like image 235
Sanjay Avatar asked Aug 06 '15 02:08

Sanjay


People also ask

Does spring-boot work with Java 8?

Spring Boot 2.7. 5 requires Java 8 and is compatible up to and including Java 19.

Can we run spring-boot application without @SpringBootApplication?

It's not mandatory to put @SpringBootApplication to create a Spring Boot application, you can still use @Configuration and @EnableAutoConfiguration individually as shown in the example given in the next point.

What does SpringApplication run () do?

SpringApplication#run bootstraps a spring application as a stand-alone application from the main method. It creates an appropriate ApplicationContext instance and load beans. It also runs embedded Tomcat server in Spring web application.


2 Answers

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>
like image 69
approxiblue Avatar answered Nov 23 '22 17:11

approxiblue


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>
like image 26
yuranos Avatar answered Nov 23 '22 17:11

yuranos