首页 > 编程语言 > 详细

SpringBoot实战

时间:2020-10-30 10:05:53      阅读:43      评论:0      收藏:0      [点我收藏+]

1、前端

2、后端

2.1、pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

2.2、application.yml

spring:
  thymeleaf:
    mode: HTML
  profiles:
    active: dev

2.2.1、application-dev.yml

开发环境

server:
  port: 8080

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/blog?UseUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    username: root
    password: admin
  jpa:
    hibernate:
      ddl-auto: update #实体类发生变化时,数据库自动更新
    show-sql: true #控制台显示sql
    database-platform: org.hibernate.dialect.MySQLDialect #设置方言,不设置报错‘hibernate.dialect‘ not set

logging:
  level:
    root: info
    com.example.demo: debug
  file:
    name: log/blog-dev.log

注意:

  1. logging.file新版本已被废弃;logging.file.name和logging.file.path同时配置,只会生效logging.file.name;
    建议使用logging.file.name

  2. 可以在资源目录下配置logback-spring.xml(官方推荐)进行更详细的日志输出,文件参考 https://www.cnblogs.com/kwdlh/p/13900044.html

2.2.2、application-pro.yml

生产环境

server:
  port: 8082

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/blog?UseUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    username: root
    password: admin
  jpa:
    hibernate:
      ddl-auto: none
    show-sql: true
    database-platform: org.hibernate.dialect.MySQLDialect

logging:
  level:
    root: warn
    com.example.demo: info
  file:
    name: log/blog-dev.log

2.3、controller全局异常处理器

2.3.1、ContExceptionHandler.java

@ControllerAdvice
public class ContExceptionHandler {

    private final Logger logger= LoggerFactory.getLogger(this.getClass());

    @ExceptionHandler(Exception.class)
    public ModelAndView handler(HttpServletRequest request,Exception e){
        logger.error("Request Url: {}, Exception: {}",request.getRequestURL(),e.getMessage());
        //某些异常不用自己处理,交给springboot处理
        if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class)!=null){
            throw e;
        }
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("url",request.getRequestURL())
                .addObject("exception",e)
                .setViewName("/error/error-info");
        return modelAndView;
    }
}

2.3.2、error-info.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>error-info</title>
</head>
<body>
<h1>查看网页源代码查看错误信息</h1>

<div>
    <div th:utext="‘&lt;!--‘" th:remove="tag">
    </div>
    <div th:utext="‘Failed Request URL : ‘+ ${url}" th:remove="tag"></div>

    <div th:utext="‘Exception message : ‘ + ${exception.message}"
         th:remove="tag"></div>
    <ul th:remove="tag">
        <li th:each="stackTrace : ${exception.stackTrace}" th:remove="tag">
            <span th:utext="${stackTrace}" th:remove="tag"></span>
        </li>
    </ul>
    <div th:utext="‘--&gt;‘" th:remove="tag"></div>
</div>

</body>
</html>

2.3.3、BlogNotFoundException.java

自定义异常

@ResponseStatus(HttpStatus.NOT_FOUND)
public class BlogNotFoundException extends RuntimeException{
    public BlogNotFoundException() {
        super();
    }

    public BlogNotFoundException(String message) {
        super(message);
    }

    public BlogNotFoundException(String message, Throwable cause) {
        super(message, cause);
    }
}

SpringBoot实战

原文:https://www.cnblogs.com/kwdlh/p/13900035.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!