Created
April 8, 2015 14:15
-
-
Save jalp/226a7ed222e7796e90e9 to your computer and use it in GitHub Desktop.
Custom Error Controller for Spring Boot(MVC)
This file contains 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 characters
import org.apache.log4j.Logger; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.boot.autoconfigure.web.ErrorController; | |
import org.springframework.boot.autoconfigure.web.ErrorAttributes; | |
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.HashMap; | |
import java.util.Map; | |
@RestController | |
public class CustomErrorController implements ErrorController { | |
private static final String PATH = "/error"; | |
private static final Logger logger = Logger.getLogger(ApiController.class); | |
@Autowired | |
private ErrorAttributes errorAttributes; | |
@Value("${application.debug}") | |
private boolean debug; | |
@Override | |
public String getErrorPath() { | |
return PATH; | |
} | |
@RequestMapping(value = PATH) | |
public Map<String, Object> error(HttpServletRequest request, HttpServletResponse response) { | |
Map<String, Object> map = new HashMap<>(); | |
map.put("status", response.getStatus()); | |
map.put("reason", getErrorAttributes(request, debug)); | |
logger.warn(map); | |
return map; | |
} | |
private Map<String, Object> getErrorAttributes(HttpServletRequest request, boolean includeStackTrace) { | |
RequestAttributes requestAttributes = new ServletRequestAttributes(request); | |
return errorAttributes.getErrorAttributes(requestAttributes, includeStackTrace); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment