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 | 29 | 30 | 31 |
Tags
- 알고리즘
- 노트북 서버
- 백준 1389 자바
- 백준 유기농 배추
- 백준 2615
- 퇴사
- 1012 자바
- 자바
- 1389 JAVA
- ubuntu
- React Native
- 백준 5430자바
- 5430 java
- BFS
- Expo
- 백준 1012 java
- 1697 자바
- 5430자바
- 백준
- 백준 1697 자바
- 1012 java
- 백준 1012 자바
- mobaXTerm
- 1389자바
- Ubuntu USB부팅
Archives
- Today
- Total
삽질메모장
spring boot SimpleMappingExceptionResolver(ajax/api/default 페이지 설정) 본문
FrameWork/spring
spring boot SimpleMappingExceptionResolver(ajax/api/default 페이지 설정)
shovel 2022. 11. 23. 19:19
@Bean
public CommExceptionAjaxResolver createSimpleMappingExceptionResolver() {
CommExceptionAjaxResolver r = new CommExceptionAjaxResolver();
r.setExceptionAttribute("ex");
r.setDefaultErrorView("/WEB-INF/jsp/errorPage"); // No default
Properties mappings = new Properties();
mappings.setProperty("CommException", "/WEB-INF/jsp/responsebody");
r.setExceptionMappings(mappings); // None by default
return r;
}
public class CommExceptionAjaxResolver extends SimpleMappingExceptionResolver{
ObjectMapper mapper = new ObjectMapper();
@Override
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
String viewName =determineViewName(ex, request); //"/WEB-INF/jsp/responsebody"
System.out.println("view name : "+ viewName);
if (viewName != null) {
String reqAjaxHeader = request.getHeader("AJAX");
if(reqAjaxHeader != null && reqAjaxHeader.equals("true")){
response.setStatus(500);
ModelAndView mav = new ModelAndView();
mav.setViewName(viewName);
mav.addObject("errMsg", ex.getMessage());
System.out.println("test"+ mav.getViewName());
return mav;
}
return getModelAndView(viewName, ex);
} else {
if(request.getRequestURI().startsWith("/api/")) {
try(ServletOutputStream sout = response.getOutputStream()){
sout.print(mapper.writeValueAsString(ApiCommResultVo.createFail(ex.getMessage())));
sout.flush();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}else {
return getModelAndView("/WEB-INF/jsp/errorPage", ex);
}
}
}
}