知识点:
1、SpringBoot常见启动方式讲解和部署war项目Tomcat
1)、ide启动
2)、jar包方式启动
maven插件:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
如果没有加,则执行jar包 ,报错如下:
java -jar spring-boot-demo-0.0.1-SNAPSHOT.jar
no main manifest attribute, in spring-boot-demo-0.0.1-SNAPSHOT.jar
如果有安装maven 用 mvn spring-boot:run
项目结构
目录结构讲解
https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#executable-jar-jar-file-structure
3)、war包方式启动
a)在pom.xml中将打包形式 jar 修改为war
<packaging>war</packaging>
构建项目名称:
<finalName>aaron_springboot</finalName>
b)tocmat下载
https://tomcat.apache.org/download-90.cgi
c)修改启动类
public class AaronSpringbootApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(AaronSpringbootApplication.class); } public static void main(String[] args) throws Exception { SpringApplication.run(AaronSpringbootApplication.class, args); } }
4)打包项目,启动tomcat
1.创建异常处理类
package net.aaron.demo.domain; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; @RestControllerAdvice public class CustomExtHandler { private static final Logger LOG =LoggerFactory.getLogger(CustomExtHandler.class); // 捕获全局异常,处理所有不可知的异常 @ExceptionHandler(value = Exception.class) Object handleException(Exception e, HttpServletRequest request) { LOG.error("url {}, msg {}", request.getRequestURL(), e.getMessage()); Map<String, Object> map = new HashMap<>(); map.put("code", 100); map.put("msg", e.getMessage()); map.put("url", request.getRequestURL()); return map; } /** * 功能描述:自定义异常处理类 * @return */ @ExceptionHandler(value=MyException.class) Object handleMyException(MyException e,HttpServletRequest request){ //进行页面跳转 // ModelAndView modelAndView = new ModelAndView(); // modelAndView.setViewName("error.html"); // modelAndView.addObject("msg", e.getMessage()); // return modelAndView; //返回json数据,由前端去判断加载什么页面 Map<String, Object> map = new HashMap<>(); map.put("code", e.getCode()); map.put("msg", e.getMsg()); map.put("url", request.getRequestURL()); return map; } }
2.自定义异常类
package net.aaron.demo.domain; /** * 功能描述:自定义异常类 * */ public class MyException extends RuntimeException { public MyException(String code, String msg) { this.code = code; this.msg = msg; } private String code; private String msg; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
3.测试异常用Controller
package net.aaron.demo.controller; import java.util.Date; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import net.aaron.demo.domain.MyException; import net.aaron.demo.domain.User; @RestController public class ExceptionController { /** * 功能描述:模拟全局异常 * @return */ @RequestMapping(value = "/api/v1/test_ext") public Object index() { int i= 1/0; return new User(11, "sfsfds", "1000000", new Date()); } /** * 功能描述:模拟自定义异常 * @return */ @RequestMapping("/api/v1/myext") public Object myexc(){ throw new MyException("499", "my ext异常"); } }
4.测试全局异常
5.测试自定义异常
6.若返回error页面,可在templates加入
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
template
<h1>出异常啦</h1>
</body>
</html>