Forked from jonikarppinen/CustomErrorController.java
Created
June 15, 2017 14:32
Revisions
-
Joni Karppinen revised this gist
Dec 12, 2015 . No changes.There are no files selected for viewing
-
Joni Karppinen revised this gist
Mar 19, 2015 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ package com.company.project.controllers; import com.company.project.model.api.ErrorJson; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.web.ErrorAttributes; @@ -33,10 +33,10 @@ public class CustomErrorController implements ErrorController { private ErrorAttributes errorAttributes; @RequestMapping(value = PATH) ErrorJson error(HttpServletRequest request, HttpServletResponse response) { // Appropriate HTTP response code (e.g. 404 or 500) is automatically set by Spring. // Here we just define response body. return new ErrorJson(response.getStatus(), getErrorAttributes(request, debug)); } @Override -
Joni Karppinen revised this gist
Feb 20, 2015 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -15,6 +15,9 @@ import java.util.Map; /** * Based on the helpful answer at http://stackoverflow.com/q/25356781/56285, * with error details in response body added. * * @author Joni Karppinen * @since 20.2.2015 */ -
Joni Karppinen renamed this gist
Feb 20, 2015 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -31,7 +31,8 @@ public class CustomErrorController implements ErrorController { @RequestMapping(value = PATH) Error error(HttpServletRequest request, HttpServletResponse response) { // Appropriate HTTP response code (e.g. 404 or 500) is automatically set by Spring. // Here we just define response body. return new Error(response.getStatus(), getErrorAttributes(request, debug)); } -
Joni Karppinen created this gist
Feb 20, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ package com.company.project.controllers; import com.company.project.model.api.Error; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.web.ErrorAttributes; import org.springframework.boot.autoconfigure.web.ErrorController; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Map; /** * @author Joni Karppinen * @since 20.2.2015 */ @RestController public class CustomErrorController implements ErrorController { private static final String PATH = "/error"; @Value("${debug}") private boolean debug; @Autowired private ErrorAttributes errorAttributes; @RequestMapping(value = PATH) Error error(HttpServletRequest request, HttpServletResponse response) { // Appropriate HTTP response code (e.g. 404 or 500) is automatically set by Spring. Here we just define response body. return new Error(response.getStatus(), getErrorAttributes(request, debug)); } @Override public String getErrorPath() { return PATH; } private Map<String, Object> getErrorAttributes(HttpServletRequest request, boolean includeStackTrace) { RequestAttributes requestAttributes = new ServletRequestAttributes(request); return errorAttributes.getErrorAttributes(requestAttributes, includeStackTrace); } }