Skip to content

Instantly share code, notes, and snippets.

@Bradleykingz
Created April 4, 2018 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bradleykingz/76492c58a70733bba3c641a3615ab690 to your computer and use it in GitHub Desktop.
Save Bradleykingz/76492c58a70733bba3c641a3615ab690 to your computer and use it in GitHub Desktop.
Removing Whitelabel Page in Spring 2.0
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;
}
}
@Bradleykingz
Copy link
Author

Based on this gist with a few modifications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment