2.2 创建maven目录结构,以及pom.xml文件
<parent >
<groupId >org.springframework.boot</groupId >
<artifactId >spring-boot-starter-parent</artifactId >
<version >2.0.5.RELEASE</version >
<relativePath />
</parent >
2.4 pom.xml文件中加入springboot-starter依赖
<dependencies >
<dependency >
<groupId >org.springframework.boot</groupId >
<artifactId >spring-boot-starter-web</artifactId >
</dependency >
<dependency >
<groupId >org.projectlombok</groupId >
<artifactId >lombok</artifactId >
</dependency >
<dependency >
<groupId >org.springframework.boot</groupId >
<artifactId >spring-boot-starter-aop</artifactId >
</dependency >
<dependency >
<groupId >com.alibaba</groupId >
<artifactId >fastjson</artifactId >
<version >1.2.83</version >
</dependency >
</dependencies >
2.5 pom.xml文件中加入maven-springboot打包插件
<build >
<plugins >
<plugin >
<groupId >org.springframework.boot</groupId >
<artifactId >spring-boot-maven-plugin</artifactId >
</plugin >
</plugins >
</build >
import org .springframework .boot .SpringApplication ;
import org .springframework .boot .autoconfigure .SpringBootApplication ;
@ SpringBootApplication
public class Application {
public static void main (String [] args ) {
SpringApplication .run (Application .class , args );
}
}
import lombok .AllArgsConstructor ;
import lombok .Data ;
@ Data
@ AllArgsConstructor
public class User {
private Integer id ;
private String name ;
private Integer age ;
}
import com .alibaba .fastjson .JSONObject ;
import javax .servlet .http .HttpServletRequest ;
import java .util .Enumeration ;
public class ParametersUtils {
public static String getParameters (HttpServletRequest req ) {
Enumeration <String > enums = req .getParameterNames ();
JSONObject parameter = new JSONObject ();
while (enums .hasMoreElements ()) {
String name = enums .nextElement ();
parameter .put (name , req .getParameter (name ));
}
return parameter .toJSONString ();
}
}
import com .oven .util .ParametersUtils ;
import lombok .extern .slf4j .Slf4j ;
import org .aspectj .lang .annotation .AfterReturning ;
import org .aspectj .lang .annotation .Aspect ;
import org .aspectj .lang .annotation .Before ;
import org .aspectj .lang .annotation .Pointcut ;
import org .springframework .stereotype .Component ;
import org .springframework .web .context .request .RequestContextHolder ;
import org .springframework .web .context .request .ServletRequestAttributes ;
import javax .servlet .http .HttpServletRequest ;
@ Slf4j
@ Aspect
@ Component
public class WebLogAspect {
@ Pointcut ("execution(public * com.oven.controller..*.*(..))" )
public void webLog () {
}
@ Before ("webLog()" )
public void doBefore () {
// 获取请求
ServletRequestAttributes attributes = (ServletRequestAttributes ) RequestContextHolder .getRequestAttributes ();
if (attributes == null ) {
return ;
}
HttpServletRequest request = attributes .getRequest ();
// 记录请求内容
log .info ("请求地址:" + request .getRequestURL ().toString ());
log .info ("请求方法:" + request .getMethod ());
log .info ("请求者IP:" + request .getRemoteAddr ());
log .info ("请求参数:" + ParametersUtils .getParameters (request ));
}
@ AfterReturning (returning = "ret" , pointcut = "webLog()" )
public void doAfterReturning (Object ret ) {
log .info ("返回结果:" + ret .toString ());
}
}
import com .oven .entity .User ;
import org .springframework .web .bind .annotation .RequestMapping ;
import org .springframework .web .bind .annotation .RestController ;
@ RestController
public class DemoController {
@ RequestMapping ("/test" )
public User test (User user ) {
user .setAge (18 );
return user ;
}
}