preface
Now there are more and more articles about Spring Boot in various technical communities, more and more pictures and videos related to Spring Boot, and more and more Internet companies are using Spring Boot. Java programmers are now out interviewing, and Spring Boot has become a must-ask.
Spring Boot is proving to be a must-have skill for Java programmers. And it is foreseeable that the development of Spring Boot will be even better in the future. Learn Spring Boot, no delay!
Spring Boot2 tutorial
Environmental requirements:
1. Create the project
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> < version > 5.1.6. RELEASE < / version > < / dependency > < the dependency > < groupId > javax.mail. Servlet < / groupId > < artifactId > javax.mail. The servlet API - < / artifactId > < version > 4.0.1 < / version > < scope > provided < / scope > < / dependency >Copy the code
2. Add Spring configuration
@Configuration
@ComponentScan(basePackages = "org.javaboy", useDefaultFilters = true,
excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes =
Controller.class)})
public class SpringConfig {
}Copy the code
Add the SpringMVC configuration
@Configuration
@ComponentScan(basePackages = "org.javaboy",useDefaultFilters =
false,includeFilters = {@ComponentScan.Filter(type =
FilterType.ANNOTATION,classes = Controller.class)})
public class SpringMVCConfig {
}Copy the code
4. Configure web.xml
public class WebInit implements WebApplicationInitializer { public void onStartup(ServletContext servletContext) throws ServletException {/ / first to load the configuration file for SpringMVC AnnotationConfigWebApplicationContext CTX = new AnnotationConfigWebApplicationContext(); ctx.register(SpringMVCConfig.class); / / add the DispatcherServlet ServletRegistration. Dynamic for springmvc = servletContext. AddServlet ("springmvc", new DispatcherServlet(ctx)); // Add path mapping to DispatcherServlet springmvc.addMapping("/"); / / add DispatcherServlet start timing for springmvc setLoadOnStartup (1); }}Copy the code
@Configuration
@ComponentScan(basePackages = "org.javaboy")
public class SpringMVCConfig {
}Copy the code
5, test,
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "hello"; }}Copy the code
Spring Boot Global exception handling
Static exception page
Dynamic exception page
<! DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>5xx</h1>
<table border="1">
<tr>
<td>path</td>
<td th:text="${path}"></td>
</tr>
<tr>
<td>error</td>
<td th:text="${error}"></td>
</tr>
<tr>
<td>message</td>
<td th:text="${message}"></td>
</tr>
<tr>
<td>timestamp</td>
<td th:text="${timestamp}"></td>
</tr>
<tr>
<td>status</td>
<td th:text="${status}"></td>
</tr>
</table>
</body>
</html>Copy the code
A 500 error occurred –> Find dynamic 500.html page –> find static 500.html –> Find dynamic 5xx.html–> find static 5xx.html.
Custom exception data
@Override
public Map<String, Object> getErrorAttributes(ServerRequest request,
boolean includeStackTrace) {
Map<String, Object> errorAttributes = new LinkedHashMap<>();
errorAttributes.put("timestamp", new Date());
errorAttributes.put("path", request.path());
Throwable error = getError(request);
HttpStatus errorStatus = determineHttpStatus(error);
errorAttributes.put("status", errorStatus.value());
errorAttributes.put("error", errorStatus.getReasonPhrase());
errorAttributes.put("message", determineMessage(error));
handleException(errorAttributes, determineException(error),
includeStackTrace);
return errorAttributes;
}Copy the code
1. Directly implement the ErrorAttributes interface
Specific definitions are as follows:
@Component
public class MyErrorAttributes extends DefaultErrorAttributes {
@Override
public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean
includeStackTrace) {
Map<String, Object> map = super.getErrorAttributes(webRequest,
includeStackTrace);
if ((Integer)map.get("status") == 500) {
map.put("message"."Server internal error!);
}
returnmap; }}Copy the code
Custom exception view
@RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
public ModelAndView errorHtml(HttpServletRequest request,
HttpServletResponse response) {
HttpStatus status = getStatus(request);
Map<String, Object> model =
Collections.unmodifiableMap(getErrorAttributes(
request, isIncludeStackTrace(request,
MediaType.TEXT_HTML)));
response.setStatus(status.value());
ModelAndView modelAndView = resolveErrorView(request, response, status,
model);
return(modelAndView ! = null) ? modelAndView : new ModelAndView("error",
model);
}Copy the code
@Override
public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus
status,
Map<String, Object> model) {
ModelAndView modelAndView = resolve(String.valueOf(status.value()),
model);
if (modelAndView == null && SERIES_VIEWS.containsKey(status.series())) {
modelAndView = resolve(SERIES_VIEWS.get(status.series()),
model);
}
return modelAndView;
}Copy the code
@Component
public class MyErrorViewResolver extends DefaultErrorViewResolver {
public MyErrorViewResolver(ApplicationContext applicationContext,
ResourceProperties resourceProperties) {
super(applicationContext, resourceProperties);
}
@Override
public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus
status, Map<String, Object> model) {
return new ModelAndView("/aaa/123", model); }}Copy the code
conclusion
Concern public number: programmer chase wind, reply 005 to get this Spring Boot actual combat tutorial.