-1

Anybody has an idea what's going on? Why I can't delete my Car?

I have the following error in the web page: There was an unexpected error (type=Bad Request, status=400). Parameter conditions "idCar" not met for actual request parameters:

These code is work if I will write the following address into web browser bar: http://localhost:8080/cars-web/deleteCar?idCar=2"

This is Code from CarGui with adnotation @Controller

@GetMapping(value = "/cars-web/deleteCar", params = "idCar")
public String deleteCar(@RequestParam long idCar) {
    carService.deleteCar(idCar);
    return "redirect:/cars-web";
}

This is the code from my CarService

@Override
public boolean deleteCar(long id) {
    Optional<Car> found = carList.stream().filter(car -> car.getCarId()== id).findFirst();

        if(found.isPresent()) {
            carList.remove(found.get());
            return true;
        } else {
            return false;
        }
}

And here is my code from my web page:

<form th:action="@{/cars-web/deleteCar}" th:object="${someId}" method="get">
<p>Id:<select>
    <option th:each="ids : ${readId2}"
            th:value="${ids}" th:text="${ids}"></option>
</select></p>
<p><input type="submit" value="Delete Car From List"></p>

Additionally my model

@GetMapping("/cars-web")
public String getCars(Model model) {
    model.addAttribute("carList", carService.getAllCars());
    model.addAttribute("readId", carService.getListId());
    model.addAttribute("readId2", carService.getListId());
    model.addAttribute("addCar", new Car());
    model.addAttribute("color", carService.getAllCarColors());
    model.addAttribute("someId", "");
    return "cars-web";
}
dopfz
  • 11
  • 1
  • 1
    Guessing the equals and hashcode methods are not overridden on your Car object? – RobOhRob Oct 21 '19 at 18:34
  • @dopfz what error appears in your log? – Juliano Pacheco Oct 21 '19 at 18:46
  • I knew that I missed something to write: I had the following error: There was an unexpected error (type=Bad Request, status=400). Parameter conditions "idCar" not met for actual request parameters: – dopfz Oct 21 '19 at 20:03
  • It seems that the problem is on your js code, you're not sending a Long parameter. – Villat Oct 21 '19 at 21:08
  • UNfortunately problem is the same after changing primitive type to object Long. – dopfz Oct 21 '19 at 21:22
  • Can I have other Controller with RESTful methods in the same project? Why I ask because I have two Controller class. First one is a RESTful controller and other one is Thymeleaf Controller. – dopfz Oct 21 '19 at 21:24

1 Answers1

0

Change:

public String deleteCar(@RequestParam long idCar) {

To:

public String deleteCar(@PathVariable("idCar") long idCar) {

and change:

@GetMapping(value = "/cars-web/deleteCar", params = "idCar")

to:

@GetMapping(value = "/cars-web/{idCar}")

You can read more about the different between PathVariable and RequestParam HERE.

yotam hadas
  • 702
  • 3
  • 14
  • I have found a solution of my problem. – dopfz Oct 22 '19 at 20:25