Home > Net >  Spring boot: view unable to reach controller and having "400 BAD_REQUEST"
Spring boot: view unable to reach controller and having "400 BAD_REQUEST"

Time:06-16

I try to do a java web application using: SpringBoot Mysql JDBC Design pattern: MVC, DAO And Thymeleaf And I'm trying to send a data from one of my views:

                    <td th:text="${Inj.sleepTest}"></td>
                    <td th:text="${Inj.sleepDose}"></td>
                    <td th:text="${Inj.nightTest}"></td>
                    <td th:text="${Inj.comment}"></td>
                    <td>
                        <form th:action="@{/delInj}" method="post">
                            <input type="hidden" id="id_injection" name="id_injection" value="${Inj.id_injection}">
                            <input  type="submit" value="Submit">
                        </form>
                    </td>

to my controler:

@RequestMapping(value="/delInj", method= RequestMethod.POST)
    public ModelAndView delinject(Injection inj){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("userOnly/MyInjections");
        int i = inj.getId_injection();
        System.out.println(i);

        return mv;

    }

but i have the error "400 BAD_REQUEST - Bad Request" in my browser. I tried with "@RequestMapping" and "PostMapping" but neither of them seams to work

CodePudding user response:

You have not added @RequestBody annotation to the method.

Like,

public ModelAndView delinject(@RequestBody Injection inj){
    ModelAndView mv = new ModelAndView();
    mv.setViewName("userOnly/MyInjections");
    int i = inj.getId_injection();
    System.out.println(i);

    return mv;

}

CodePudding user response:

Bad Request In general means: that the structure of your Request Body does not match the JSON sent (may be Types, constraints, structure ...) check the JSON you sent in the request and the expected Request Body "Model"

  • Related