You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring项目中DeleteMapping等注解导入失败问题求助

解决Spring 4.0.1中无法导入DeleteMapping等注解的问题

问题根源

你碰到的「The import org.springframework.web.bind.annotation.DeleteMapping cannot be resolved」错误,本质是你的Spring版本(4.0.1.RELEASE)不支持这些注解。@DeleteMapping、@GetMapping、@PostMapping、@PutMapping这些简化的请求映射注解,是Spring在4.3版本才正式推出的,4.0.x系列根本没有这些类,所以IDE会提示无法解析。


两种可行解决方案

方案1:升级Spring版本(推荐)

既然这些注解是4.3+才有的,最彻底的解决办法就是把Spring版本升级到4.3.x及以上(比如选一个稳定的4.3.x版本,或者直接升级到更现代的Spring 5.x系列)。只需要修改pom.xml里的版本属性:

<properties>
    <!-- 替换为4.3.30.RELEASE或更高版本,这里选一个4.3系列的稳定版 -->
    <spring.version>4.3.30.RELEASE</spring.version>
</properties>

修改后执行mvn clean install刷新Maven依赖,IDE就能正常识别这些注解了。

方案2:用传统@RequestMapping注解替代(无需升级)

如果暂时不想升级Spring版本,完全可以用4.0.x支持的@RequestMapping注解来实现相同功能,只需要指定method参数即可:
首先替换导入语句:

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

然后在控制器方法上这样写:

// 等价于@GetMapping("/your-api")
@RequestMapping(value = "/your-api", method = RequestMethod.GET)
public String getResource() {
    // ...
}

// 等价于@PostMapping("/your-api")
@RequestMapping(value = "/your-api", method = RequestMethod.POST)
public String createResource() {
    // ...
}

// 等价于@PutMapping("/your-api/{id}")
@RequestMapping(value = "/your-api/{id}", method = RequestMethod.PUT)
public String updateResource(@PathVariable Long id) {
    // ...
}

// 等价于@DeleteMapping("/your-api/{id}")
@RequestMapping(value = "/your-api/{id}", method = RequestMethod.DELETE)
public String deleteResource(@PathVariable Long id) {
    // ...
}

这样就能在不升级版本的前提下,完成相同的请求映射逻辑。


内容的提问来源于stack exchange,提问作者Explain Down Vote

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:49:49