现在开始项目的创建
文章目录
一.构建框架
1.创建项目
并引入:
- devtools
- web
- thymeleaf
- jpa
- mysql
2.配置项目
(1).修改yarm文件
修改thymeleaf的版本
<properties>
<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>
(2).修改application.yml文件
application.yml
spring:
thymeleaf:
mode: HTML
profiles:
active: dev
application-dev.yml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf-8
username: root
password: guan37
jpa:
hibernate:
ddl-auto: update
show-sql: true
logging:
level:
root: info
net.ty: debug
file.path: log/blog-dev.log
application-pro.yml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf-8
username: root
password: guan37
jpa:
hibernate:
ddl-auto: none
show-sql: true
logging:
level:
root: warn
net.ty: info
file.path: log/blog-pro.log
server:
port: 880
二. 异常和日志处理
1.异常处理
(1).新建异常处理页面
新建error页面并新建404和500页面
(2).新建index页面以及运行它的程序
>>indexController
package net.ty.web;
import net.ty.NotFoundException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* @date 2021年06月16日 10:05
*/
@Controller
public class indexController {
@GetMapping("/")
public String index() {
//
return "index";
}
}
>>handler/ControllerExceptionHandler
package net.ty.handler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
/**
* @date 2021年06月16日 10:08
*/
@ControllerAdvice
public class ControllerExceptionHandler {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@ExceptionHandler(Exception.class)
public ModelAndView exceptionHandler(HttpServletRequest request, Exception e) throws Exception {
logger.error("Requst URL : {}, Exception : {}", request.getRequestURI(),e);
if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) {
throw e;
}
ModelAndView mv = new ModelAndView();
mv.addObject("url",request.getRequestURL());
mv.addObject("exception", e);
mv.setViewName("error/error");
return mv;
}
}
>>NotFoundException
import org.springframework.web.bind.annotation.ResponseStatus;
/**
* @date 2021年06月16日 11:13
*/
@ResponseStatus(HttpStatus.NOT_FOUND)
public class NotFoundException extends RuntimeException {
public NotFoundException() {
}
public NotFoundException(String message) {
super(message);
}
public NotFoundException(String message, Throwable cause) {
super(message, cause);
}
}
(3).实现效果:
2.异常处理
>>Aspect/LogAspect
package net.ty.Aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
/**
* @date 2021年06月16日 11:26
*/
@Aspect
@Component
public class LogAspect {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Pointcut("execution(* net.ty.web.*.*(..))")
public void log(){}
@Before("log()")
public void doBefore(JoinPoint joinPoint){
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String url = request.getRequestURL().toString();
String ip = request.getRemoteAddr();
String classMethods = joinPoint.getSignature().getDeclaringTypeName() + '.' + joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
RequestLog requestLog = new RequestLog(url, ip, classMethods, args);
logger.info("request: {}" , requestLog);
}
@After("log()")
public void doAfter() {
}
@AfterReturning(returning = "result",pointcut = "log()")
public void doAfterReturning(Object result){
logger.info("Result : {}", result);
}
private class RequestLog {
private String url;
private String ip;
private String classMethod;
private Object[] args;
private RequestLog(String url, String ip, String classMethod, Object[] args) {
this.url = url;
this.ip = ip;
this.classMethod = classMethod;
this.args = args;
}
@Override
public String toString() {
return "{" +
"url='" + url + '\'' +
", ip='" + ip + '\'' +
", classMethod='" + classMethod + '\'' +
", args=" + Arrays.toString(args) +
'}';
}
}
}
indexController
package net.ty.web;
import net.ty.NotFoundException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* @date 2021年06月16日 10:05
*/
@Controller
public class indexController {
@GetMapping("/{id}/{name}")
public String index(@PathVariable Integer id, @PathVariable String name) {
// String blog = null;
// if (blog ==null) {
// throw new NotFoundException("博客不存在");
// }
System.out.println("----------index-----------");
return "index";
}
}