오류&에러/Spring

스프링 RequestMethod.GET?POST? 405에러

씨네 2021. 4. 22. 10:19
728x90

스프링을 공부하던 어느날... 갑자기 Request method 'POST' not supported 405에러를 만났습니다.

 

405에러는 뭔가를 보낸거 같기는 한데 받아주는애가 이거를 할수 없어요~ 하는 에러라고 합니다!

 

왜 그런가했더니

@RequestMapping(value="/command.do", method=RequestMethod.POST)
public String getCommand(Model model, @RequestParam("name") String name, String addr, String phone) {
		
	model.addAttribute("dto", new AddressDto(name, addr, phone));
		
	return "get";
}

테스트해본다고 POST방식으로 전달을 했더니 405에러가 뜨고 다시 GET방식으로 바꿔봤더니

아주 잘 실행됩니다...

 

그래서

<form action="command.do" method="post">
	이름:<input type="text" name="name"/><br>
	주소:<input type="text" name="addr"/><br>
	번호:<input type="text" name="phone"/><br>
	<input type="submit" value="전송"/>
</form>

index에서 해당 코드로

@RequestMapping(value="/command.do", method=RequestMethod.POST)
public String postCommand(Model model, @ModelAttribute AddressDto dto) {
		
	model.addAttribute("dto", dto);
		
	return "post";
}
	

컨트롤러에 이렇게 받아주니까

이건 아주 잘나옵니다!

728x90