Created
April 4, 2018 20:25
-
-
Save Bradleykingz/76492c58a70733bba3c641a3615ab690 to your computer and use it in GitHub Desktop.
Removing Whitelabel Page in Spring 2.0
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 characters
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController; | |
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver; | |
import org.springframework.boot.web.servlet.error.ErrorAttributes; | |
import org.springframework.boot.web.servlet.error.ErrorController; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.util.List; | |
import java.util.Map; | |
@RestController | |
public class SimpleErrorController extends AbstractErrorController { | |
private static final String PATH = "/error"; | |
@Autowired | |
public SimpleErrorController(ErrorAttributes errorAttributes) { | |
super(errorAttributes); | |
} | |
public SimpleErrorController(ErrorAttributes errorAttributes, List<ErrorViewResolver> errorViewResolvers) { | |
super(errorAttributes, errorViewResolvers); | |
} | |
@RequestMapping(value = PATH) | |
public ResponseEntity<Map<String, Object>> error(HttpServletRequest aRequest, HttpServletResponse response) { | |
Map<String, Object> body = getErrorAttributes(aRequest, getTraceParameter(aRequest)); | |
String trace = (String) body.get("trace"); | |
if (trace != null) { | |
String[] lines = trace.split("\n\t"); | |
body.put("trace", lines); | |
} | |
return new ResponseEntity<>(body, HttpStatus.valueOf(response.getStatus())); | |
} | |
protected boolean getTraceParameter(HttpServletRequest request) { | |
String parameter = request.getParameter("trace"); | |
return parameter != null && !"false".equals(parameter.toLowerCase()); | |
} | |
@Override | |
public String getErrorPath() { | |
return PATH; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on this gist with a few modifications.