2023.05.03 - [Spring] - spring vue 연동 상품 관리,게시판 관리 어드민 페이지 03

 

spring vue 연동 상품 관리,게시판 관리 어드민 페이지 03

에러코드: error: "Method Not Allowed" message: "Request method 'PATCH' not supported" path: "/index.html" status: 405 timestamp: "2023-05-03T14:30:05.217+00:00" trace: "org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PAT

kwaksh2319.tistory.com

이전글에서 patch시 405 에러가 나왔는데요 

백엔드에서는 문제가 없이 200 ok가 되었습니다.

 그래서 백엔드에서 return을 할때 index.html( view를 빌드 하였기 때문에 index.html을 이용) redirect를 했는데 여기서 

라우터가 못찾는것 같더라고요. 그래서 해결법으로는 리턴을 성공시 200ok보내주고 실패시 bad request를 보내주기로 판단 하였습니다.
해결법 : return ResponseEntity.ok().build();

@PatchMapping("/api/Notice/Patch/{id}")
    public ResponseEntity<?> updatePatch(@PathVariable Long id, @RequestBody Notice newNotice) {
        try{
            System.out.println("/api/Notice/Patch");
            noticeService.update(id,newNotice);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok().build();
    }

이렇게하면 axios에서 올바르게 response 되었다 판단하여 try catch쪽으로 넘어가지않고 올바르게 됩니다. 여기서 

vue를 새로고침하였습니다. 

this.axios.patch(`/api/Notice/Patch/${postId}`, data,config)
		  .then(response => {
             alert('게시판 수정 완료');
             this.$router.go(0);
           })
           .catch(error => {
             alert('게시판 수정 실패');
             console.log(error);
 });

올바르게 작동하였습니다.

 

 

+ Recent posts