我正在开发一个包括CRUD功能的应用程序。我使用Spring作为后端实现,Angular作为前端实现。在前端,我像这样使用Angular的http客户端来删除一个实体(为了简洁起见,以下代码被缩短了):
组件:
export class RecipeComponent implements OnInit {
constructor(private recipeService : RecipeService) { }
deleteElement (id : number) {
this.recipeService.deleteRecipe(id).subscribe();
}
}服务:
export class RecipeService {
deleteRecipe (id : number) : Observable<{}> {
return this.http.delete('http://localhost:8080/recipe/' + id);
}
constructor( private http: HttpClient ) { }
}在执行函数时,我可以在network选项卡中找到http-delete请求:

Spring控制器看起来(简称)如下:
@ControllerAdvice
@CrossOrigin(origins = "http://localhost:4200")
@RequestMapping(path="/recipe")
public class RESTController {
@Autowired
private RecipeRepository recipeRepository;
@DeleteMapping(path="/{id}")
public @ResponseBody ResponseEntity deleteRecipe (@RequestBody Recipe deletedRecipe, @PathVariable("id") int id) {
try {
[...]
}
} catch (RecipeNotFoundException e) {
[...]
}
}
}然而,我的后端没有收到请求。奇怪的是,发布或获取工作都很好。
谁能给我指个方向?
谢谢你,祝你好运!
发布于 2020-03-25 05:13:12
400 Bad request很可能是"body is missing“错误。从方法参数中删除@RequestBody Recipe deletedRecipe由于您没有提供要删除的实体,因此id就足够了。
https://stackoverflow.com/questions/60838773
复制相似问题