当前位置: 首页> 文旅> 美景 > web of science官网_软件开发的三种基本方法_互联网舆情信息_网络推广公司专业网络

web of science官网_软件开发的三种基本方法_互联网舆情信息_网络推广公司专业网络

时间:2025/7/9 1:49:21来源:https://blog.csdn.net/qq_40949254/article/details/146516699 浏览次数:0次
web of science官网_软件开发的三种基本方法_互联网舆情信息_网络推广公司专业网络

环境版本

  • JDK21
  • spring-webmvc 6.1.14

问题现象

在这里插入图片描述


🔍 先说错误原因

@PathVariable 使用问题(Java高版本17+存在问题)

原始方法:

@GetMapping("/{id}")
public Book getBookById(@PathVariable Integer id) {return bookService.getBookById(id);
  1. 方法参数未指定 @PathVariable("id")
    • Spring 无法解析参数名称,需要手动指定
  2. Maven 编译未启用 -parameters 参数
    • Spring 需要这个参数来自动推断参数名称,否则必须手动写 @PathVariable("id")

✅ 解决方案

1️⃣ 显式指定 @PathVariable(“id”)

你的方法可能是这样:

@GetMapping("/{id}")
public Book getBookById(@PathVariable Integer id) {  // ❌ 可能会报错return bookService.getBookById(id);
}

请修改为:

@GetMapping("/{id}")
public Book getBookById(@PathVariable("id") Integer id) { // ✅ 显式指定 "id"return bookService.getBookById(id);
}

2️⃣ 启用 -parameters 编译参数

如果不想手动写 @PathVariable("id"),你需要修改 Maven 配置:

🔹 修改 pom.xml
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>21</source>  <!-- 或者 17,根据你的 JDK 版本 --><target>21</target><compilerArgs><arg>-parameters</arg>  <!-- 重点! --></compilerArgs></configuration></plugin></plugins>
</build>

📌 结论

🎯 必须满足以下两点之一

方式 1(手动写 @PathVariable("id")
方式 2(在 pom.xml 里加 -parameters 参数)

关键字:web of science官网_软件开发的三种基本方法_互联网舆情信息_网络推广公司专业网络

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: