Spring Boot provides an /error mapping by default that handles all errors in a sensible way, and it is registered as a ‘global’ error page in the servlet container. For machine clients it will produce a JSON response with details of the error, the HTTP status and the exception message. For browser clients there is a ‘whitelabel’ error view that renders the same data in HTML format (to customize it just add a View that resolves to ‘error’). To replace the default behaviour completely you can implement ErrorController and register a bean definition of that type, or simply add a bean of type ErrorAttributes to use the existing mechanism but replace the contents.
Spring Boot 以一个合理的方式提供了默认的映射 /error ,用于处理所有错误。并且它被作为一个全局的错误页注册在servlet容器中。对于机器客户端,它将产生一个带有详细错误信息的JSON响应,HTTP状态和异常信息。对于浏览器客户端,将会返回一个白色的错误标签页,同样的错误信息以HTML格式渲染(想要自定义这个页面,只需添加一个解析“error”的View)。要完全地替换这个默认的行为,你可以实现 ErrorController ,并且注册一个bean定义这个类型,或者简单的增加一个 ErrorAttributes 类型的bean来使用现有的机制,但替换掉内容。
The BasicErrorController can be used as a base class for a custom ErrorController. This is particularly useful if you want to add a handler for a new content type (the default is to handle text/html specifically and provide a fallback for everything else). To do so, extend BasicErrorController, add a public method with a @RequestMapping that has a produces attribute, and create a bean of your new type.
In the preceding example, if YourException is thrown by a controller defined in the same package as FooController, a JSON representation of the CustomErrorType POJO is used instead of the ErrorAttributes representation.
If you want to display a custom HTML error page for a given status code, you can add a file to an /error folder. Error pages can either be static HTML (that is, added under any of the static resource folders) or built by using templates. The name of the file should be the exact status code or a series mask.
publicclassMyErrorViewResolverimplementsErrorViewResolver {@Overridepublic ModelAndView resolveErrorView(HttpServletRequest request,
HttpStatus status, Map model) {
// Use the request or status to optionally return a ModelAndViewreturn ...
}
}
You can also use regular Spring MVC features such as @ExceptionHandler methods and @ControllerAdvice. The ErrorController then picks up any unhandled exceptions.
For applications that do not use Spring MVC, you can use the ErrorPageRegistrar interface to directly register ErrorPages. This abstraction works directly with the underlying embedded servlet container and works even if you do not have a Spring MVC DispatcherServlet.
N.B. if you register an ErrorPage with a path that will end up being handled by a Filter (e.g. as is common with some non-Spring web frameworks, like Jersey and Wicket), then the Filter has to be explicitly registered as an ERROR dispatcher, e.g.
注:如果你注册一个最终会由一个Filter处理的路径的ErrorPage(例如:常见的一些非Spring web 框架,像 Jersey 和 Wicket),那么必须将Filter显示的注册为一个 ERROR 调度器。
@Bean
public FilterRegistrationBean myFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new MyFilter());
...
registration.setDispatcherTypes(EnumSet.allOf(DispatcherType.class));
return registration;
}
(the default FilterRegistrationBean does not include the ERROR dispatcher type).
(默认的 FilterRegistrationBean 不包括 ERROR 调度器类型)
Error Handling on WebSphere Application Server
When deployed to a servlet container, Spring Boot uses its error page filter to forward a request with an error status to the appropriate error page. The request can only be forwarded to the correct error page if the response has not already been committed. By default, WebSphere Application Server 8.0 and later commits the response upon successful completion of a servlet’s service method. You should disable this behavior by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false.
当部署到一个Servlet容器时,Spring Boot使用其错误页面过滤器转发带有错误状态的请求到合适的错误页。如果response还没有提交,那么请求只能被转发的正确的错误页面。默认的,WebSphere Application Server 8.0及更高版本在成功完成servlet的服务方法后提交响应。你应该禁用这个行为,通过设置 com.ibm.ws.webcontainer.invokeFlushAfterService 为 false。
web.xml报错
The content of element type "web-app" must match "(icon?,display-
name?,description?,distributable?,context-param*,filter*,filter-mapping*,listener*,servlet*,s
JUnit4:Test文档中的解释:
The Test annotation supports two optional parameters.
The first, expected, declares that a test method should throw an exception.
If it doesn't throw an exception or if it
借鉴网上的思路,用java实现:
public class NoIfWhile {
/**
* @param args
*
* find x=1+2+3+....n
*/
public static void main(String[] args) {
int n=10;
int re=find(n);
System.o
在Linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory。
分析:这是不同系统编码格式引起的:在windows系统中编辑的.sh文件可能有不可见字符,所以在Linux系统下执行会报以上异常信息。
解决:
1)在windows下转换:
利用一些编辑器如UltraEdit或EditPlus等工具
Binary search tree works well for a wide variety of applications, but they have poor worst-case performance. Now we introduce a type of binary search tree where costs are guaranteed to be loga