티스토리 뷰
api 요청이 실패할 경우에는 html 으로 실패값이 와서는 안된다. 일반 예외와 같이 boot는 BasicError 컨트롤러를 이용해서 api 실패 예외처리도 해준다.
Request문서의 Accept가 application/json일 경우 (html이 아닐 경우 아래의 메소드로 작동한다.)
1. html 일경우
2. 그 외
@RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
HttpStatus status = getStatus(request);
Map<String, Object> model = Collections
.unmodifiableMap(getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.TEXT_HTML)));
response.setStatus(status.value());
ModelAndView modelAndView = resolveErrorView(request, response, status, model);
return (modelAndView != null) ? modelAndView : new ModelAndView("error", model);
}
@RequestMapping
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
HttpStatus status = getStatus(request);
if (status == HttpStatus.NO_CONTENT) {
return new ResponseEntity<>(status);
}
Map<String, Object> body = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL));
return new ResponseEntity<>(body, status);
}
server.error.whitelabel.enabled=false
server.error.include-exception=true
server.error.include-message=on_param
server.error.include-stacktrace=on_param
server.error.include-binding-errors=on_param
여기에 그치지 않고 application.properties에서 설정을 변경해주면 더 자세한 값을 볼 수 있다.
server.error.whitelabel.enabled=false
server.error.include-exception=true
server.error.include-message=always
server.error.include-stacktrace=always
server.error.include-binding-errors=always
하지만 이렇게 길게 오류로그를 남기면 보안상 문제가 있을 수 있으니, 사용하지 않는 것이 바람직하다.
'dev > spring boot + intelliJ' 카테고리의 다른 글
예외 자동변환하는 DefaultHandlerExceptionResolver (0) | 2023.12.08 |
---|---|
HandlerExceptionResolver로 예외처리 깔끔하게 처리하기 (1) | 2023.12.05 |
api 예외처리는 html이면 안된다. (0) | 2023.12.03 |
스프링 부트는 자동으로 에러페이지를 작성해준다. BasicErrorController (1) | 2023.12.02 |
Interceptor를 이용할 때에 오류페이지는 타지않는 방법 (0) | 2023.12.02 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- BindingResult
- jpa api
- 항해99
- 스프링공부
- ArgumentResolver
- reject
- React
- react실행
- 예외처리
- 리터럴
- SpringBoot
- 로그인
- Java
- 항해플러스
- thymleaf
- hypertexttransferprotocol
- 백엔드 개발자 공부
- HTTP
- 향해플러스
- 향해99
- JPA
- 향해플러스백엔드
- exception
- 백엔드 개발자 역량
- rejectValue
- filter
- 스프링부트
- Intercepter
- 컨트
- 인터셉터
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 |
글 보관함