常用配置

打开调试

We will use the default Spring Boot configuration file that was created for us and put it in the debug mode. Add the following line to src/main/resources/ application.properties:

debug=true
1

Now, if we launch our application again we'll see Spring Boot's autoconfiguration report. It is divided into two parts: positive matches, which list all autoconfigurations that are used by our application; and negative matches, which are Spring Boot autoconfigurations whose requirements weren't met when the application started:

@ContionalConClass: https://blog.csdn.net/lucytheslayer/article/details/80430912

WebMvcAutoConfiguration

视图解析

@Configuration
@EnableWebMvc
public class ApplicationWebMvcConfig extends WebMvcConfigurerAdapter{

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/jsp/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

静态资源

ResourceProperties类

	private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
			"classpath:/META-INF/resources/", "classpath:/resources/",
			"classpath:/static/", "classpath:/public/" };
1
2
3

DispatcherServletAutoConfiguration

ErrorMvcAutoConfiguration

spring Boot提供了错误回退处理页面。

在启动时,Spring Boot试图找到/ error的映射。按照惯例,以/error 结尾的URL映射到同名的逻辑视图:error。在引入Thymeleaf依赖后,此视图则映射到error.html Thymeleaf模板。 (如果使用JSP,根据InternalResourceViewResolver的设置,它将映射到error.jsp)。实际的映射将取决于pring Boot设置的ViewResolver(如果有)。

如果没有找到view-resolver 的/error映射,Spring Boot会定义它自己的回退错误页面 - 的“Whitelabel错误页面”(一个只包含HTTP状态信息和任何错误细节的最小页面,例如来自未捕获异常的消息)。

如果正在创建RESTful请求(HTTP请求已指定除HTML之外的所需响应类型),Spring Boot将返回JSON表示,其中包含与“Whitelabel”错误页面中相同的错误信息。

$> curl -H "Accept: application/json" http://localhost:8080/no-such-page

{"timestamp":"2018-04-11T05:56:03.845+0000","status":404,"error":"Not Found","message":"No message available","path":"/no-such-page"}
1
2
3

关闭whitelabel

server.error.whitelabel.enabled=false 使用tomcat默认

###自定义错误页面

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 be built by using templates. The name of the file should be the exact status code or a series mask.

Springboot 2.0 与之前的配置不太一样,具体参考文档:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-error-handling-custom-error-pages

自定义errorController

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/error")
public class SimpleErrorController implements ErrorController {

    private final ErrorAttributes errorAttributes;

    @Autowired
    public SimpleErrorController(ErrorAttributes errorAttributes) {
        Assert.notNull(errorAttributes, "ErrorAttributes must not be null");
        this.errorAttributes = errorAttributes;
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }

    @RequestMapping
    public Map<String, Object> error(HttpServletRequest aRequest){
        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);
        }
        body.put("site","Freshal.cc");
        return body;
    }

    private boolean getTraceParameter(HttpServletRequest request) {
        String parameter = request.getParameter("trace");
        if (parameter == null) {
            return false;
        }
        return !"false".equals(parameter.toLowerCase());
    }

    private Map<String, Object> getErrorAttributes(HttpServletRequest aRequest, boolean includeStackTrace) {
        ServletWebRequest requestAttributes = new ServletWebRequest(aRequest);
        return errorAttributes.getErrorAttributes(requestAttributes, includeStackTrace);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

https://gist.github.com/jonikarppinen/662c38fb57a23de61c8b

ThymeleafAutoConfiguration

EmbeddedServletContainerAutoconfiguration

Spring Boot includes support for embedded Tomcat, Jetty, and Undertow servers. Most developers use the appropriate “Starter” to obtain a fully configured instance. By default, the embedded server listens for HTTP requests on port 8080. 更换容器

	compile('org.springframework.boot:spring-boot-starter-web')
	compile('org.springframework.boot:spring-boot-starter-jetty')
	configurations {
		compile.exclude module: 'spring-boot-starter-tomcat'
	}
1
2
3
4
5

Actuator

http方式访问jmx

#修改默认管理url
management.endpoints.web.base-path=/manage
#启用jolokia
management.endpoint.jolokia.enabled=true
#暴露服务端点
management.endpoints.web.exposure.include=*
1
2
3
4
5
6