SpringBoot实战【个人博客】
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
注意:
-
logging.file新版本已被废弃;logging.file.name和logging.file.path同时配置,只会生效logging.file.name;
建议使用logging.file.name -
可以在资源目录下配置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="'<!--'" 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="'-->'" 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);
}
}
2.4、日志处理
2.4.1、LogAspect.java
@Aspect
@Component
public class LogAspect {
private Logger logger= LoggerFactory.getLogger(this.getClass());
@Pointcut("execution(* com.example.demo.controller..*.*(..))")
public void log(){}
@Before("log()")
public void before(JoinPoint joinPoint){
HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder
.getRequestAttributes(),"RequestAttributes为null!")).getRequest();
String url = request.getRequestURL().toString();
String ip= request.getRemoteAddr();
String ClassMethod=joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName()+"()";
Object[] args=joinPoint.getArgs();
RequestLog requestLog = new RequestLog(url,ip,ClassMethod,args);
logger.info("RequestLog: {}",requestLog);
}
@AfterReturning(pointcut = "log()",returning = "result")
public void afterReturning(Object result){
logger.info("Result: {}",result);
}
@Data
@AllArgsConstructor
@NoArgsConstructor
private class RequestLog{
private String url;
private String ip;
private String classMethod;
private Object[] args;
}
}

浙公网安备 33010602011771号