“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:
- SpringMVc @RequestMapping url of regular expression pattern
- How to Solve JAR pack error: Error resolving template [/userInfo], template might not exist or might not be accessib
- Spring MVC uses Ajax to submit requests asynchronously to complete login
- Spring boot uses thread pool to realize asynchronous processing without return and asynchronous processing with return
- How to Solve Tomcat Error: Could not resolve view with name ‘xxx/xxxxxxx‘ in servlet with
- Using Post no Body Error: socket hang up [How to Solve]
- Xdoc generates API documents based on Java annotations
- Asynchronous processing of HTTP request by Java_ Method 2: through deferredresult
- Asynchronous processing of HTTP request by Java_ Method 1: through callable
- CORS error has been blocked by CORS policy [How to Solve]
- [Solved] Java.lang.IllegalStateException: getReader() has already been called for this request
- After asynchronous file import and springboot multipartfile upload, the @async asynchronous processing reports an error: nosuchfileexception
- Java Error: No enclosing instance of type Main is accessible. Must qualify the allocation with an encl
- [Solved] spring boot Startup Error: Error creating bean with name ‘requestMappingHandlerMapping‘ defined in class path
- [Solved] Circular view path [index]: would dispatch back to the current handler URL [] again. Che
- [Solved] Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;
- [Solved] org.thymeleaf.exceptions.TemplateInputException: Error resolving template
- Abstract method and static method of java interface
- How to convert a Java string into a number (stringtonumber)
- Request processing failed; nested exception is java.lang.NullPointerException or UnsatisfiedDependencyE