【发布时间】:2021-01-21 04:41:30
【问题描述】:
我正在开发一个 CRUD 应用程序,它将在 SpringBoot 部分中提供 REST(它也将有一个 Angular 部分,使用 JSON)。 SpringBoot 部分提供优雅的 JSON(针对 MySQL 数据库的查询),但是当我运行尝试删除记录的部分时,我收到 405 错误:
“出现意外错误(类型=不允许的方法,状态=405)。”
这是失败的代码(它正在调用@Service)
@RequestMapping(value = "/avisos/delete/{id}", method = RequestMethod.DELETE)
public ResponseEntity<Void> borraAviso(@RequestParam("id") Long id) {
boolean isRemoved;
isRemoved = avisoService.borraAviso(id);
if (!isRemoved) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
else
return new ResponseEntity<>(HttpStatus.OK);
}
这是 CORS 配置文件:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class CorsConfiguration implements WebMvcConfigurer
{
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:4200")
//.allowedMethods("GET", "POST");
.allowedMethods("");
}
}
该程序在 Linux Mint 盒子中运行,但我也在 W8 盒子中进行了测试,我得到了同样的错误。 (我使用的是 Spring Tool Suite 4,版本:4.8.0.RELEASE 和 Maven)。
【问题讨论】:
标签: spring-boot cors