2023/05/08 spring 2

김홍규·2023년 5월 9일
0

ResDTO = 화면에 내보낼 정보
(code/message/data)

서버 상태 코드

200 ok

요청이 성공적으로 완료되었습니다. (GET)

201 Created

요청이 성공적이었으며 그 결과 새로운 리소스가 생성되었습니다.(POST)

400 Bad Request

이 응답은 잘못된 문법으로 인하여 서버가 요청을 이해할 수 없습니다.

401 Unauthorized

비록 HTTP 표준에서는 "미승인(Unauthorized)"를 명확히 하고 있지만 의미상 이 응답은
"비인증"을 의미합니다.
로그인 안하고 로그인한 데이터를 요청했을 때 나타남

403 Forbidden

클라이언트는 콘텐츠에 접근할 권리를 가지고 있지 않습니다. 예를들어 그들은 미승인이어서
서버는 거절을 위한 적절한 응답을 보냅니다. 401과 다른점은 서버가 클라이언트가 누구인지 알고 있다.
어드민페이지에 일반유저가 들어온 경우

405 Method Not Allowed

요청한 메소드는 서버에서 알고 있지만, 제거되었고 사용할 수 없습니다.

500 Internal Server Error

서버가 처리 방법을 모르는 상황이 발생했습니다.

HttpEntity
ResponseEntity

  • 구성 : (상태코드 (200,400,500,...)/ 헤더(Cookie,...) / 바디(ResDTO))
    REST Controller 로 데이터 리턴 시 무조건
    ResponseEntity + ResDTO 을 사용한다.

AOP

  • validation ,ErrorHanling 도 AOP의 일종
    -스프링에서 제공하는 것이 아닌 나만의 커스텀 AOP를 만들려면 라이브러리 추가
    -클래스에 @Component / @Aspect 추가
  • 상세 설명 https://pamyferret.tistory.com/51

@Aspect
@Component
public class CommonAdvice {
    // 경로를 맞춰줌으로써 그 내부에 조건에 부합하는게 작동했을 때 작동하도록
	//pointcut(경로) 지정 방법
	// @Before("execution(public * com.example.my..controller..*(..))")
    // @Before("within(com.example.my..controller.*)")
    // @Before("bean(*ControllerApi*)")
    // public void tempAdvice() {
    // log.warn("요청이들어왔습니다.");
    // }

    @Around("execution(* com.example.my.todo.repository..findByIdx(..))")
    public Object checkFindByIdx(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

        // proceedingJoinPoint.proceed() ->repository의 findByIdx 함수를 의미
        Object result = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());

        if (result == null) {
            throw new EntityNotFoundException("해당하는 데이터가 없습니다.");
        }
        return result;
    }
}

0개의 댓글

관련 채용 정보