ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • HTTP DELETE 처리
    [공부] 프로그래밍/Spring・Spring Boot (JAVA) 2024. 4. 2. 11:36

     

    HTTP DELETE 처리
    private Map<String, Object> delete(String _id, IF_Token _if_Token) throws Exception {
            Map<String, Object> returnMapinsert = new HashMap<>();
            try {
                HttpURLConnection httpURLConnection = null;
                try {
                    //connection
                    URL url = new URL(config.get base URL().concat(Const_Url.toString().concat(_id)));
                    httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setInstanceFollowRedirects(false);
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setRequestMethod("DELETE");
    
                    // header information
                    CommonUtil.setheader(httpURLConnection, _if_Token, domainService.getDomainName(), domainService.getUserID());
    
                    httpURLConnection.connect();
    
                    int httpStatusCode = -1;
    
                    try {
                        returnMapinsert = do_delete_id(httpURLConnection);
    
                    } catch (Exception e) {
                        try {
                            httpStatusCode = httpURLConnection.getResponseCode();
    
                        } catch (Exception e2) {
                            httpStatusCode = -1;
                        }
                        returnMapinsert = do_Err(e, httpURLConnection, httpStatusCode);
    
                    } finally {
                        httpURLConnection.disconnect();
                    }
    
                } catch (Exception e) {
                    LOG.error(e.getMessage());
                } finally {
                    httpURLConnection.disconnect();
                }
    
            } catch (Exception e) {
                LOG.error(e.getMessage());
            }
            return returnMapinsert;
        }
    
        private Map<String, Object> do_delete_id(HttpURLConnection httpURLConnection) throws Exception {
            return CommonUtil.setResponseString(httpURLConnection, "");
        }
    
        private Map<String, Object> do_Err(Exception e, HttpURLConnection httpURLConnection, int httpStatusCode) throws Exception {
            return CommonUtil.errMap(e, httpURLConnection, httpStatusCode);
        }
    ◇ delete 메서드:
    ・ _id: 삭제할 리소스를 식별하는 ID입니다.
    ・ _if_Token: 인증 토큰 객체입니다.
    ・ returnMapinsert: 삭제 작업 결과를 저장할 맵 객체입니다.
    ・ 메서드는 Exception을 던질 수 있습니다.
    메서드 내부에서는 다음과 같은 작업들을 수행합니다:
    ・ try-catch 블록 내부에서 HTTP 연결 설정 및 DELETE 요청을 합니다.
    ・ 헤더 정보를 설정합니다.
    ・ DELETE 요청을 서버에 전송합니다.
    ・ 응답 코드를 확인하고 적절한 처리를 합니다.

    ◇ do_delete_id 메서드: httpURLConnection:
    ・ DELETE 요청을 보낸 HttpURLConnection 객체입니다.
    이 메서드는 CommonUtil.setResponseString 메서드를 호출하여 HTTP 응답을 처리하고 결과를 반환합니다.

    ◇ do_Err 메서드: e:
    ・ 예외 객체입니다.
    ・ httpURLConnection: DELETE 요청을 보낸 HttpURLConnection 객체입니다.
    ・ httpStatusCode: HTTP 응답 코드입니다.
    이 메서드는 CommonUtil.errMap 메서드를 호출하여 예외와 응답 상태를 처리하고 결과를 반환합니다.

     

    '[공부] 프로그래밍 > Spring・Spring Boot (JAVA)' 카테고리의 다른 글

    세션 처리  (0) 2024.04.03
    FreeMarker 설정  (0) 2024.04.03
    암호화, 복호화 처리  (0) 2024.03.14
    enum  (0) 2024.03.05
    Repository 테이블 조작  (0) 2024.03.01
Designed by Tistory.