Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- transaction
- pointcut
- 스프링 핵심 원리
- spring
- Android
- Exception
- 자바
- Servlet
- jpa
- 김영한
- kotlin
- Thymeleaf
- db
- Proxy
- java
- springdatajpa
- JPQL
- Spring Boot
- JDBC
- 스프링
- QueryDSL
- 스프링 핵심 기능
- 알고리즘
- 인프런
- http
- SpringBoot
- AOP
- 그리디
- Greedy
- 백준
Archives
- Today
- Total
개발자되기 프로젝트
[Server] Exception Resolver 본문
현재 유저가 data를 잘못 입력할 경우 IllegalArgumentException이 발생한다.
이 경우는 유저가 잘 못 요청하여 발행한 예외이기 때문에 500이 아니라 400을 return하고자 한다.
ExceptionResolver를 사용하면 간단히 해결이 가능하다.
[API예외] HandlerExceptionResolver
예외가 발생해서 서블릿을 넘어 WAS까지 예외가 전달되면 HTTP 상태코드가 500으로 처리된다. 발생하는 예외에 따라서 400, 404 등등 다른 상태코드로 처리하고 싶은데? 오류 메시지, 형식등을 AP
bsh-developer.tistory.com
1. ExceptionResolver
IllegalArgumentException의 경우 400으로 return
@Slf4j
public class MyExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
//IllgalArgsException인 경우 400으로return
try{
if (ex instanceof IllegalArgumentException){
log.info("[MyExceptionResolver] IllegalArgumentException to 400");
response.sendError(HttpServletResponse.SC_BAD_REQUEST, ex.getMessage());
return new ModelAndView(); //정상 흐름으로 전환.
}
} catch (IOException e) {
log.error("[MyExceptionResolver] IOException 발생", e);
}
return null;
}
}
2. 등록
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
resolvers.add(new MyExceptionResolver());
}
}
3.Test
다음과 같이 빈 data를 보내보자. 이 경우 IllegalArgumentException이 발생한다.
의도대로 400이 return되었다.
Log에도 400으로 return된 것을 확인할 수 있다.
[Google Client Error] Status: BAD_REQUEST
[Google Client Error] errorMessage: Invalid request. Missing the 'address', 'components', 'latlng' or 'place_id' parameter.
[MyExceptionResolver] IllegalArgumentException to 400
4. GitHub: 220205 GoogleGeoCoding ErrorMessage, ExceptionHandler
GitHub - bsh6463/commuteMap
Contribute to bsh6463/commuteMap development by creating an account on GitHub.
github.com
'Project > 대중교통 길찾기' 카테고리의 다른 글
[Server] 실시간 지하철 도착정보 연동 (0) | 2022.02.10 |
---|---|
Field값 검증 (0) | 2022.02.07 |
[Server] Google Geocoding Error처리 (0) | 2022.02.05 |
[Server] Oday Error 처리2 (0) | 2022.02.04 |
[Server] Oday Error 처리 (0) | 2022.02.03 |
Comments