Reputation: 167
I am working on an App including CRUD-functionality. I use Spring for the backend implementation, Angular for the frontend. In the frontend, I use Angular's http client like this to delete an entity (The following code is shortened for the sake of brevity):
Component:
export class RecipeComponent implements OnInit {
constructor(private recipeService : RecipeService) { }
deleteElement (id : number) {
this.recipeService.deleteRecipe(id).subscribe();
}
}
Service:
export class RecipeService {
deleteRecipe (id : number) : Observable<{}> {
return this.http.delete('http://localhost:8080/recipe/' + id);
}
constructor( private http: HttpClient ) { }
}
When executing the function, I can find the http-delete request in the network tab:
The Spring Controller looks (shortend) like this:
@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) {
[...]
}
}
}
Yet, my backend does not receive the request. The strange thing is, Posts or Gets work fine.
Can anyone point me to the right direction?
Thanks and best!
Upvotes: 0
Views: 297
Reputation: 31805
The 400 Bad request
is very likely a "body is missing" error. Remove @RequestBody Recipe deletedRecipe
from the method parameters since you don't provide the entity to delete, the id
is enough.
Upvotes: 1