0

Having some difficulties in dealing with CRUD delete method on a Spring based project. Keep getting an 404 error. Due to the fact of me being a newbie to programming I'm not able to figure out the problem on my own. I tried to seek some solutions on various YouTube guides and articles, tried to re-write, but nothing helped. Huge thanks in advance to anyone who could help me out. Attached some portion of code that is dealing with DELETE method of CRUD:

Service:

public void deleteVisit(Long id) {
    repository.delete(id);
}

Controller:

@DeleteMapping("delete-appointment/{id}")
public void deleteAppointment(@PathVariable Long id) {
    dentistVisitRepository.delete(id);
}

Thymeleaf:

<td><a th:href="@{delete-appointment/{id}(id=${appointment.id})}">Delete</a></td>
natt
  • 3
  • 1
  • Isn't that a GET in your html? – Roddy of the Frozen Peas Apr 27 '21 at 21:29
  • 1
    I haven't used Thymeleaf, but generally an 'href' is not going to invoke a `delete` call. So is there some Javascript that does the translation to the `Delete`? [This question here](https://stackoverflow.com/questions/24256051/delete-or-put-methods-in-thymeleaf) seems to also suggest something more is needed. – KevinO Apr 27 '21 at 21:29
  • Thanks. SavedBeau offered the similar way of solving this, but keep getting an 404. – natt Apr 28 '21 at 13:44

2 Answers2

1
  1. In the controller you need to call the service
@DeleteMapping("delete-appointment/{id}")
public String deleteAppointment(@PathVariable Long id) {

    //You might have to first find it before deletion to ensure there are no errors
    appointmentService.deleteVisit(id);
    return "Deleted";
}
  1. In the Service you call deleteById from repository
public void deleteVisit(Long id) {
    repository.deleteById(id);
}
  1. The default HTTP method used by anchor tag is GET you need to create a form to be able to change the http method
<form action="#" th:action="@{delete-appointment/{id}'(id=${appointment.id})}" th:method="delete" >
  <input type="hidden" name="_method" value="delete" />
  <button type="submit" id="submitButton"> </button>
</form>

If you are using spring boot 2.2+, add following into you application.properties

spring.mvc.hiddenmethod.filter.enabled=true

After spring boot 2.2+, this hidden method filter is disabled by default. For more details check here.

Ratul Sharker
  • 7,484
  • 4
  • 35
  • 44
Tinashe
  • 1,052
  • 12
  • 20
  • Tried to rewrite your solution, but keep getting an 404 error, unfortunately. Will it be any use of posting my entire project? – natt Apr 28 '21 at 13:41
  • Forgot to say, that my CRUD repository doesn't have a deleteById method, so I'm using delete instead. – natt Apr 28 '21 at 13:49
  • 1
    @natt i have updated the answer, hope you are using spring boot 2.2+ – Ratul Sharker Apr 29 '21 at 06:39
-1

this code take object parameter repository.delete(Entity)

but this code take Long Parameter used this code to delete by id

  public void deleteVisit(Long id) {
    repository.deleteById(id);
}

if you want delete by entity you can use this code

  public void deleteVisit(Long id) {
   Entity entity=repository.findById(id).get();
    repository.delete(entity);
}
  • While possibly accurate, how does this address the 404 error? Because I would think the original code wouldn't compile or it would just fail to delete the object. – KevinO Apr 27 '21 at 21:30
  • i am written this code if your id actually exist in you data base but you can check it when check if present like this `if(repository.findById(id).present())` @KevinO – Abdalrhman Alkraien Apr 27 '21 at 21:34