【问题标题】:How to compile Spring Boot applications with Java 8 --parameter flag如何使用 Java 8 --parameter 标志编译 Spring Boot 应用程序
【发布时间】:2015-10-29 00:09:15
【问题描述】:

Spring documentation 告诉我们,如果我们使用 Java 8 --parameters 标志编译我们的项目,我们可以跳过在 @PathVariable 等注释中提供参数名称。这意味着,我们可以只使用@PathVariable id 而不是@PathVariable("id") id

在 Spring Boot Maven 应用程序中,我很想知道如何告诉编译器使用 parameters 标志。它是默认开启的吗?我们需要在 pom.xml 中提供一些东西吗?

【问题讨论】:

    标签: spring spring-mvc spring-boot


    【解决方案1】:

    在 Spring Boot 2.0 中,--parameters 标志应默认启用。见yuranos87's answer

    对于旧版本,在 pom.xml 文件中,您可以将 Java 编译器选项指定为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>
    

    【讨论】:

      【解决方案2】:

      我不记得在我的任何项目中都需要明确地这样做。也许您只需要添加 spring-boot-starter-parent (我知道,有时可能不是一个选项)。否则,Spring 已经为您处理好了一切。

      在 Spring Boot 文档中多次提到。例如here:

      为了允许将输入映射到操作方法的参数,实现端点的代码应该使用 -parameters 编译。如果您使用 Spring Boot 的 Gradle 插件或使用 Maven 和 spring-boot-starter-parent,这将自动发生。

      更新

      way Spring Boot 做得非常简单(在 spring-boot-parent 和 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>
      

      【讨论】:

      • 哦,太好了。知道这是在 Boot 2.0 中添加的还是之前存在的?在 Boot 1.4 Gradle 项目中,它不适合我。
      • 我会检查 maven Spring BOM,因为我确信它已经存在了一段时间并且我们依赖它很长时间了,但对于 gradle 来说,它似乎是最近才添加的:github.com/spring-projects/spring-boot/blob/…。此行来自提交:“在 Gradle 构建中默认使用 -parameters 编译器 arg”,2017 年 9 月 20 日
      • @Sanjay,想象一下我的惊喜。对于 maven,情况类似。它是最近添加的:github.com/spring-projects/spring-boot/blob/master/… 2017 年 7 月 31 日的“默认情况下使用 Maven 启用“-parameters”编译器标志。现在我需要弄清楚我们过去是如何配置它的:)
      • 不错的发现!作为参考,这里是 Maven/Kotlin buildsGradle builds 的拉取请求。
      猜你喜欢
      • 2018-06-24
      • 2018-02-08
      • 2016-04-28
      • 2021-06-27
      • 1970-01-01
      • 1970-01-01
      • 2021-02-09
      • 2021-05-27
      相关资源
      最近更新 更多