rest controller practice : controller log(aop)
This commit is contained in:
File diff suppressed because it is too large
Load Diff
BIN
rest-controller-practice/logs/spring-jpa.log.2021-03-16.0.gz
Normal file
BIN
rest-controller-practice/logs/spring-jpa.log.2021-03-16.0.gz
Normal file
Binary file not shown.
@@ -1,9 +1,11 @@
|
||||
package com.example.restcontroller.board.controller;
|
||||
|
||||
import com.auth0.jwt.exceptions.JWTVerificationException;
|
||||
import com.example.restcontroller.board.entity.Board;
|
||||
import com.example.restcontroller.board.entity.BoardType;
|
||||
import com.example.restcontroller.board.model.*;
|
||||
import com.example.restcontroller.board.service.BoardService;
|
||||
import com.example.restcontroller.common.exception.BizException;
|
||||
import com.example.restcontroller.common.model.ResponseResult;
|
||||
import com.example.restcontroller.notice.model.ResponseError;
|
||||
import com.example.restcontroller.user.model.ResponseMessage;
|
||||
@@ -178,6 +180,18 @@ public class ApiBoardController {
|
||||
|
||||
return ResponseResult.result(result);
|
||||
}
|
||||
|
||||
@GetMapping("/api/board/{id}")
|
||||
public ResponseEntity<?> chapter4_3(@PathVariable Long id) {
|
||||
|
||||
Board board = null;
|
||||
try {
|
||||
board = boardService.detail(id);
|
||||
} catch (BizException e) {
|
||||
return ResponseResult.fail(e.getMessage());
|
||||
}
|
||||
return ResponseResult.success(board);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -54,4 +54,18 @@ public class Board {
|
||||
private LocalDate publishStartDate;
|
||||
private LocalDate publishEndDate;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Board{" +
|
||||
"id=" + id +
|
||||
", user=" + user +
|
||||
", boardType=" + boardType +
|
||||
", title='" + title + '\'' +
|
||||
", content='" + content + '\'' +
|
||||
", topYn=" + topYn +
|
||||
", regDate=" + regDate +
|
||||
", publishStartDate=" + publishStartDate +
|
||||
", publishEndDate=" + publishEndDate +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,4 +32,15 @@ public class BoardType {
|
||||
@JsonIgnore
|
||||
@OneToMany(mappedBy = "boardType")
|
||||
List<Board> boardList = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BoardType{" +
|
||||
"id=" + id +
|
||||
", boardName='" + boardName + '\'' +
|
||||
", regDate=" + regDate +
|
||||
", updateDate=" + updateDate +
|
||||
", usingYn=" + usingYn +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,4 +47,6 @@ public interface BoardService {
|
||||
List<Board> postList(String email);
|
||||
|
||||
List<BoardComment> commentList(String email);
|
||||
|
||||
Board detail(Long id);
|
||||
}
|
||||
|
||||
@@ -372,4 +372,10 @@ public class BoardServiceImpl implements BoardService {
|
||||
|
||||
return boardCommentRepository.findByUser(userEntity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Board detail(Long id) {
|
||||
return boardRepository.findById(id)
|
||||
.orElseThrow(() -> new BizException("게시글이 존재하지 않습니다."));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.example.restcontroller.common.aop;
|
||||
|
||||
import com.example.restcontroller.logs.service.LogService;
|
||||
import com.example.restcontroller.user.model.UserLogin;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@Aspect
|
||||
@RequiredArgsConstructor
|
||||
public class BoardLogger {
|
||||
|
||||
private final LogService logService;
|
||||
|
||||
@Around("execution(* com.example.restcontroller..*.*Controller.*(..))")
|
||||
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
|
||||
log.info("############################################################");
|
||||
log.info("컨트롤러 호출 전!");
|
||||
log.info("############################################################");
|
||||
|
||||
Object result = joinPoint.proceed();
|
||||
|
||||
if (joinPoint.getSignature().getDeclaringTypeName().contains("ApiBoardController")
|
||||
&& "chapter4_3".equals(joinPoint.getSignature().getName())) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append("파라미터 : ");
|
||||
Object[] args = joinPoint.getArgs();
|
||||
Arrays.stream(args).forEach((v) -> {
|
||||
sb.append(v.toString());
|
||||
});
|
||||
sb.append("결과 : ");
|
||||
sb.append(result.toString());
|
||||
|
||||
logService.add(sb.toString());
|
||||
log.info(sb.toString());
|
||||
}
|
||||
|
||||
log.info("############################################################");
|
||||
log.info("컨트롤러 호출 후!");
|
||||
log.info("############################################################");
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user