0

I'm learning Spring Boot and just started to see H2-Database. I managed to send a HTTP GET Request to insert a new line on my database, but I can't delete it.

This is how I inserted a new line:

@GetMapping("/addAlien")
public String addAlien(Alien alien) {
  rep.save(alien);
  return "home.jsp";
}

This is the JSP code I'm using to send the GET Request:

<form action="addAlien">
  <input type="text" name="id"><br>
  <input type="text" name="name"><br>
  <input type="text" name="points"><br>
  <input type="submit"><br>
</form>

And this is the SQL file that I'm using to fill the H2-Database

insert into alien values (101, 'Bilu', 100)
insert into alien values (102, 'Vargínia', 100)
insert into alien values (103, 'Meeseek', 100)
insert into alien values (104, 'Jack', 100)
insert into alien values (105, 'MrPopButthole', 100)

I have another form like the one that I used to send the GET Request, but to submit the "/deleteAlien". And the submit activates the code below, although it doesn't work:

@DeleteMapping("/deleteAlien")
public int deleteAlien(@RequestParam int id) {
  rep.deleteById(id);
  ModelAndView mv = new ModelAndView("showAlien.jsp");
  Alien alien = rep.findById(id).orElse(null);          
  mv.addObject(alien);
  return id;
}

After submiting I get this error here:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Sep 19 06:12:27 BRT 2019
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported...

What I want to do is specify a "int id" through a form, then delete the line that has an equal id.


SOLUTION: I managed to solve the problem using Postman to send the correct HTTP Request. It works, but I would appreciate if someone guided me to do it using a jsp.file and a form.

This is the code that works:

@DeleteMapping("/alien/{id}")
public String deleteAlien(@PathVariable("id") int id) {
  Alien a = rep.getOne(id);
  rep.delete(a);
  return "deleted";
}

I just have to open Postman and config it to send a DELETE using localhost:8080/alien/id.

jccampanero
  • 50,989
  • 3
  • 20
  • 49
  • FYI instead of using screenshots, embed those examples in the question. BTW your error indicates that you did not use the correct request method to `@DeleteMapping("/deleteAlien")` which needs `DELETE`, not `GET` – Ivar Sep 19 '19 at 08:59
  • Welcome to Stack Overflow! Please don't post images of your code. There are [several reasons](https://meta.stackoverflow.com/a/285557/6612182) that this is not encouraged on SO. – Rallen Sep 19 '19 at 09:04
  • Really sorry about the images. – Jean Achille Sep 19 '19 at 09:18

3 Answers3

1

You're sending HTTP GET request to delete the item while your controller says that the mapping is DELETE, so you need to change the HTTP request to be DELETE instead of GET

rjeeb
  • 461
  • 3
  • 11
  • I thought about it. But doesn't the "DeleteMapping" do that? I thought that everything below it would be a HTTP DELETE. – Jean Achille Sep 19 '19 at 09:22
  • `DeleteMapping` means that this url supports `HTTP` requests with `DELETE` method only, so when a request comes with different method, then `HttpRequestMethodNotSupportedException` will be thrown. – rjeeb Sep 19 '19 at 09:23
  • Oh! I thought that DeleteMapping was responsible to change the HTTP Request. When GetMapping, it would be a HTTP GET, when DeleteMapping, HTTP DELETE. Thanks! – Jean Achille Sep 19 '19 at 19:04
1

Default HTTP method is GET so you are requesting to server with GET HTTP method

instade of requesting with GET HTTP method you have to request with DELETE HTTP method

to do so

create a another jsp file with form to delete entry in that jsp file write form tag with method="DELETE" and action="deleteAlien"

<form action="deleteAlien" method="DELETE">

[input tags]

</form>
  • I did as you told but it stil doesn't work :/ I managed to delete through Postman. I'll edit the post to show the solution, but I wanted to do it through the form :'( – Jean Achille Sep 19 '19 at 19:03
  • In your code you have used @DeleteMapping("/alien/{id}") so your action URL must be something like action="alien/{id}" here {id} must be the id of entry you want to delete lets say you want to delete entry with id=1 so your action URL must be something like action="alien/1" now take some way to insert id in your action URL – Murtaza Haamid Sep 20 '19 at 05:47
  • if you are typing id of entry you want delete manually then you have to use javascript to add id in your action url if yow know the id load time of jsp file for delete the you can inject it with simple jsp code action="alien/<%=id%>" here id is a variable which contains id of entry you want to delete to know more find me on fb with my Name and ping me – Murtaza Haamid Sep 20 '19 at 05:49
0

Is there a '`' on your code before @DeleteMapping?

`@DeleteMapping("/deleteAlien")

Check here and here

Erwin
  • 460
  • 2
  • 6
  • put method="DELETE" on your jsp., I have edit my answer, please check – Erwin Sep 19 '19 at 09:42
  • Just tried. Still doesn't. I managed to delete through Postman. I'll edit the post to show the solution, but I wanted to do it through the form :'( – Jean Achille Sep 19 '19 at 18:59
  • You cannot put delete in your jsp, please check my answer, I have updated it – Erwin Sep 20 '19 at 01:57