본문 바로가기

STUDY/Spring

(79)
Mockito | @Mock, @InjectMocks MockitoExtension JUnit5는 @ExtendWith애노테이션을 사용한다. 해당 애노테이션을 사용하면 @Mock, @Spy, @InjectMocks와 같은 MockitoAnnotations를 사용할 수 있게된다. @ExtendWith(MockitoExtension.class) class SomeTestClass {}@ExtendWidth()로 설정하고 싶지 않다면 직접 해줄 수도 있다.. public class SampleBaseTestCase { private AutoCloseable closeable; @Before public void openMocks() { closeable = MockitoAnnotations.openMocks(this); } @After public void re..
MyBatis | ResultMap ResultMap을 사용하면 복잡한 결과를 객체에 간단히 매핑할 수 있다. 사용 방법은 간단하다. User라는 객체가 있을 때 아래와 같이 을 작성하면 된다. public class User { private String name; private int age; } SELECT user_name, user_age FROM USERS 만약 클래스간 포함관계, 즉 멤버변수로 다른 객체를 가지고 있다면 을 이용해 매핑해 줄 수 있다. 조인을 하거나 보다 복잡한 매핑에서 훨씬 유용하다. public class Address { private String zipcode; private String address; } public class User { private String name; private int a..
Spring Boot | Spring Security JWT 인증 처리 과정 JWT 인증 과정 정리하기 위해 작성해 본다. JWT 생성 과정은 생략한다. dependency 스프링 시큐리티와 JWT를 사용하기 위한 라이브러리를 추가한다. dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'com.auth0:java-jwt:3.18.1' } 시큐리티 설정을 해준다. 일단 configure()는 기본만.. @EnableWebSecurity로 시큐리티 사용을 설정하면, 자동으로 스프링 시큐리티에서 ..
Spring Boot | Request 마다 로그 찍기 HandlerInterceptor HandlerInterceptor 인터페이스를 구현하는 방법. preHandle()의 반환 값을 true로 설정한다. @Slf4j @Component public class CustomRequestInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HttpServletRequest requestCacheWrapperObject = new ContentCachingRequestWrapper(request); request..
Spring Boot | war 빌드, 외부 톰캣 사용 (WAS에 배포하기) Spring Boot는 내부 톰캣이 있고.. jar로 빌드한다.. 하지만... JSP를 써야했고.. war로 외부 톰캣에 배포해야 할 일이 생겼다... war로 빌드 일단 war로 빌드하는 설정은 아주 간단하다.. id 'war'를 추가해주면 와르로 와르르 plugins { id 'org.springframework.boot' version '2.5.5' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' id 'war' } 근데 이렇게만 하고 자연스럽게 배포했고, 로그에 started.. 잘 됐다고 떴는데 404에러가 떴다.. ..
Spring | JDBCTemplate 사용하기 JPA와 QueryDsl 사용 중.. FROM절 서브쿼리 조인이 필요해서 부득이하게 JdbcTemplate을 함께 사용하게 됐다. 오랜만에 사용하니까 어려워서 정리해본다.. RowMapper RowMapper 인터페이스를 구현하면 쿼리 실행 결과를 바로 POJO 객체에 매핑시킬 수 있다. @AllArgsConstructor @Getter public class AnimalResponseDto { private String name; private String breed; private int age; } 매핑시킬 코드를 작성한 후 사용하면 끝!! public class AnimalRowMapper implements RowMapper { @Override public AnimalResponseDto ma..
JPA | @Converter, @Convert (+ @Convert 먹히지 않을 때) 지금 하는 프로젝트가 레거시 디비를 이용해야 하는데.. 데이터 베이스 구조나 값을 변경할 수는 없고.. 1, 2 등에 해당하는 값을 일일이 찾아보는 것도 귀찮아서 ENUM으로 변환해서 사용하고자 한다.. ENUM 정의 예를 들어 1은 진돗개, 2는 삽살개...를 의미한다고 할 때.. 대충 이런 구조의 ENUM을 정의할 수 있다. @Getter public enum DogBreed { JINDO(1, "진돗개"), SAPSAL(2, "삽살개"); private int num; private String name; Animal(int num, String name) { this.num = num; this.name = name; } } 한 두개 일 때는 외울만 하지만 값도 여러개에 테이블마다 1, 2, 3...
Spring Boot | SMTP (worksmobile 메일) 필요한 의존성을 추가한다. implementation 'org.springframework.boot:spring-boot-starter-mail'설정 파일에 메일 발송계정 정보를 입력한다. spring: mail: host: smtp.worksmobile.com port: 587 username: 발송계정 메일주소 password: 발송계정 비밀번호 properties: mail: smtp: auth: true starttls.enable: true 회사 메일이 웍스모바일 메일을 이용하고 있기 때문에 해당 정보로 입력해줬다. 정보를 확인하는 방법은, 환경설정 > 고급설정 > IMAP/SMTP에서 확인할 수 있다. HTML태그를 이용해 메일을 발송하고자 한다면 MimeMessage를 이용해야..