[Solved] “status“:405,“error“ Request method ‘POST‘ not supported“

“status“:405,“error“ Request method ‘POST‘ not supported“

Error Messages:

-“status”:405,“error”:“Method Not Allowed”,“exception”:“org.springframework.web.HttpRequestMethodNotSupportedException”,“message”:“Request method ‘POST’ not supported”

 

code:

@Controller
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping("/empadd")
    public String empAdd(Model model) {
        model.addAttribute("list",empService.showAll());
        return "emp-add";
    }

    @PostMapping("/add")
    public String add(Emp emp, MultipartFile file){
        empService.insert(emp,file);
        return "emp-add";
    }
}

 

Reason:
As it says in the Spring REST guide,

@RequestMapping maps all HTTP operations by default

but if, as they suggest, you added a specification of the allowable http methods:

@RequestMapping(method=GET)

then only GETs will be allowed. POSTs will be disallowed.
If you want to allow both GET and POST, but disallow all other http methods, then annotate your controller method thusly:

@RequestMapping(value = "/greeting", method = {RequestMethod.GET, RequestMethod.POST})
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
    return new Greeting(counter.incrementAndGet(),
                        String.format(template, name));
}

When you start the application, all the request handler mappings are logged out. You should see a line like this in your log (in the IDE console or command line window):

s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/greeting],methods=[GET || POST]}" onto public hello.Greeting hello.GreetingController.greeting(java.lang.String)

Solution:

Modify

@GetMapping("/empadd")

to

@RequestMapping("/empadd")

Read More: