공부/Spring
[Spring Error] nested exception is java.lang.IllegalArgumentException: Could not locate appropriate constructor on class
jpql 을 사용할때 다음과 같은 에러가 나왔다. nested exception is java.lang.IllegalArgumentException: Could not locate appropriate constructor on class 원인으로는 column의 순서가 맞지 않거나 type이 맞지 않아서 생기는 오류이다.
[spring] api 호출 시 response, request 값 로그에 출력하기 - AOP 활용한 logging 출력
api 를 호출할 때마다 response, request 값의 로그를 log.info() 를 통해 작성하는 것이 비효율적이라고 느껴져서 공통적인 모듈을 사용해서 api 호출할때마다 로그를 안찍어도 출력될 수 있게 사용하고 싶었다. 로그를 남겨서 에러가 발생했을 때 빠른 대처와 api 흐름이 정상 동작하고 있는지에 대해 파악하기에도 좋다. build.gradle 에 의존성 추가 // aop implementation 'org.springframework.boot:spring-boot-starter-aop' //Joiner implementation 'com.google.guava:guava:31.1-jre' aop 를 사용하기 위해 의존성을 추가해준다. 코드에서 joiner 를 사용하기 위해선 관련 의존성..
[Spring] JPA repository 에서 count 사용하기
JPA에서 테이블 조회 시 Count 값을 가져오려면 Repository에 CountBy() 메소드를 추가해주면 되는데 만약 조회조건이 있다면 CountBy컬럼명(Param param) 과 같이 사용해주면 된다 마지막으로 count 조회 시 Long 타입으로 리턴하기 때문에 반드시 Return 데이터 타입을 Long으로 잡아줘야 한다
[spring] Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDateTime'
날짜 데이터 조회시 String 으로 데이터를 받아오고 있었고 사용해야 하는 값은 LocalDate 였다. String 으로 받아온 후 DateFormatter을 하지 않고 값을 api 에서 받아올때부터 LocalDate 로 사용했다. @RequestParam(value = "startDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE, pattern="yyyy-MM-dd") LocalDate startDate
[Spring] JPA ERROR :: Unable to locate Attribute with the the given name on this ManagedType
JPA ERROR :: Unable to locate Attribute with the the given name on this ManagedType 헤당 오류의 원인은 오타이다. DB 에 정의한 내용과 Entity 에서 선언한 컬럼명이 달라서 생기는 오류이다.
[spring] @AuthenticationPrincipal 권한 비교
controller public MemberDto findMember(@AuthenticationPrincipal U principal , @Parameter(name = "page", in = ParameterIn.QUERY) Pageable pageable ) { return MemberService.findMember(principal, pageable); } service @Override public MemberDto findMember(U principal, Pageable pageable) { memberService.findMember(pageable); // 권한 확인 if(principal.getAuthorities().toString().equals("[SUPER_ADMIN]")){ ..
[JPA] object references an unsaved transient instance - save the transient instance before flushing
JPA 연관관계 사용 중 아래와 같은 에러가 나왔다. object references an unsaved transient instance - save the transient instance before flushing 오류 이유 해당 오류가 생기는 이유는 FK 로 사용되는 컬럼값이 없는 상태에서 데이터를 넣으려다보니 생기는 에러인 것이다. 예를 들어 아래와 같은 내용이 있을 때 Member (id, name) Address (id, address1, member_id) Member에 데이터를 넣지 않고 Address 에 데이터를 넣으려고 하면 member_id 값이 없어서 에러가 발생하는 것이다. 해결 방법 해결 방법으로는 연관관계를 설정할 때 cascade 옵션을 설정해준다. cascade = Ca..
[spring jpa] repository 에서 가장 상위에 있는 id값 가져오기
jpa 에서 id를 역순 정렬해 가장 상위에 있는 id 깂을 가져오고자 한다. Top / First 어느것을 사용하던 결과는 동일하다. public interface MemberEntityRepository extends JpaRepository { MemberEntity findFirstByOrderByIdDesc(); MemberEntity findTop1ByOrderByIdDesc(); }
[Spring Boot] (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
(no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator) 다음과 같은 에러가 났다. 해당 오류는 기본 생성자를 추가해서 해결했다.
[Spring boot] modelMapper 에러 - required a bean of type 'org.modelmapper.ModelMapper' that could not be found.
modelMapper 를 사용하려는데 다음과 같은 에러가 나왔다. 에러 메시지 Parameter 1 of constructor in com.restapi.auth.service.impl.RelativeServiceImpl required a bean of type 'org.modelmapper.ModelMapper' that could not be found. 1개 이상의 자동주입 후보가 있어야 한다는 뜻으로 @Bean을 등록해준다. @Service @RequiredArgsConstructor public class MemberServiceImpl implements MemberService { private final ModelMapper modelMapper; } @Bean 등록 import org...