no image
Spring) Client와 WebSocket Server 사이를 중계하기
client와 웹소켓 서버 사이를 중계하기 = 웹소켓 서버이면서 클라이언트 역할을 수행한다. server1과 server2를 어떤 방법으로 어느 타이밍에 연결할지에 고민이 많았다. 방법에 있어서는 server2의 WebSocket Server는 다룰 수 없고 server2는 Stomp 프로토콜을 사용하고 있지 않은데 server1을 Stomp Client로 server2와 연결을 시도하며 삽질했고 타이밍에 있어서는 요청을 트리거로 server1과 server2를 연결하고 작업완료 시 연결을 종료하려 했으나 잘 안 됐었다. 결국, 구현은 server1과 server2의 ws연결은 spring websocket stomp 가 아닌 java-websocket 라이브러리를 사용했고 implementation '..
2023.12.04
no image
spring mapstruct
mapstruct dto entity 사이를 변환하는 코드를 생성해주는 패키지다. //build.gradle dependencies { ... compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' //mapstruct를 lombok 보다 뒤에 implementation 'org.mapstruct:mapstruct:1.5.5.Final' annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final' ... } getter, setter, builder 등 lombok을 사용한다면 lombok 코드 생성이 먼저 실행된 후에 mapstruct가 동작해야..
2023.10.15
no image
spring lombok SuperBuilder
SuperBuilder 생성 코드 @SuperBuilder @ToString public class BuilderTestDto { private String one; private String two; } ====================== @SuperBuilder @ToString(callSuper = true) public class BuilderTestDto2 extends BuilderTestDto{ private String three; } 부모와 자식 둘 다 붙여야하고 사용했을 때 컴파일 된 코드는 아래와 같다. public class BuilderTestDto2 extends BuilderTestDto { private String three; protected BuilderTestDt..
2023.10.08
no image
spring mongodb repository
mongodb connecting // application.properties spring.data.mongodb.host= ~~ spring.data.mongodb.port=27017 spring.data.mongodb.database=test spring.data.mongodb.username=root spring.data.mongodb.password=1234 spring.data.mongodb.authentication-database=admin authentication admin 설정 없을 시, authentication 관련 예외가 발생했다. Collection 정의 @Document(collection = "testCollection") @Getter @Setter @ToString pu..
2023.10.01
no image
JDBC, JPA, Hibernate, Spring Data JPA
JDBC (Java Database Connectivity) : DB에 접근할 수 있도록 자바에서 제공하는 API (인터페이스) JPA ( Java Persistence API ) : 자바 ORM 기술에 대한 표준 명세이다. (인터페이스) Hibernate : JPA의 구현체 중 하나 Spring Data JPA : JPA의 구현체를 한 층 더 추상화하여 비즈니스 로직에 더욱 집중한다. 다만 DB의 독립적 개발은 어려워진다. Repository 인터페이스를 사용 implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'mysql:mysql-connector-java:8.0.28' spring-data-jpa..
2023.09.28
no image
lombok @Builder
클래스 레벨 @Builder @Builder public class Test { private String one; private Long two; public Test(Long two){ this.two = two; } } lombok builder는 기본적으로 allArgsConstructor 로 동작한다. 그래서 위 코드에서 @Builder는 one, two를 인자로 받는 생성자를 사용하지만 정의되지 않은 생성자이므로 컴파일 에러가 발생한다. 정의된 건 two를 받는 생성자 뿐이다. public Test build() { return new Test(this.one, this.two); } ( 위 처럼 코드를 만들고 정의되지 않은 생성자로 에러가 발생할 것이다. ) 생성자 레벨 @Builder pu..
2023.08.06
Json 직렬/역직렬, @RequestBody, @ModelAttribute
Json 변환에서 직렬화 - json으로 변환 역직렬화 - 자바 객체로 변환 +) response dto말고 entity로 return할 때, entity에 @getter 없으면 직렬화 안된다. @RequestBody @Setter나 생성자가 없어도 동작한다.( 기본 생성자는 필요함 ) 데이터 바인딩을 위한 필드명을 알아내기 위해 getter나 setter 중 1가지는 정의되어 있어야 한다. 어노테이션을 생략할 순 없다. @ModelAttribute @Setter나 생성자가 있어야 동작한다. Query String 또는 Form형식이 아닌 데이터는 처리할 수 없다. ( application/json은 안됨 ) 어노테이션을 생략할 수 있다. 참고 자료 https://tecoble.techcourse.co...
2023.08.04
no image
spring data JPA - slice, page (무한 스크롤, 페이지네이션)
페이지를 구현한다면 두 가지 경우를 생각해볼 수 있다. 스크롤이 끝에 다다르면 추가 요청으로 정보를 불러오는 무한 스크롤과 페이지 전환으로 다음 정보를 보여주는 페이지네이션이다. 둘 다 sql의 limit, offset을 잘 활용하면 구현할 수 있어보인다. 하지만 spring data jpa에서 편리한 인터페이스를 제공한다. query method에 Pageable을 인자로 주면 return으로 Slice, Page, List 등의 타입을 받을 수 있다. public interface S3ImageRepository extends JpaRepository { Slice findSliceBy(Pageable pageable); Page findPageBy(Pageable pageable); } Pageab..
2023.07.27
no image
Spring Security 예외처리( AuthenticationEntryPoint, AccessDeniedHandler, unsuccessfulAuthentication )
관련 포스트 2023.07.13 - [Spring/Security] - 스프링) Spring Security Authentication/Authorization (23-06-29) 스프링) Spring Security Authentication/Authorization (23-06-29) velog에서 이전한 글 입니다. Filter Spring의 life cycle이다. Security Spring Security도 많은 filter로 구성 돼있다. Security의 목적은 controller에서 인증/인가를 분리하고 filter단에서 손쉽게 처리하기 위함이다. cornpip.tistory.com Spring Security 예외처리 현재 구성한 인증/인가 흐름이다. 프론트에서 error msg를 요청..
2023.07.26