반응형

Multi DB 환경에서 Transaction 어노테이션이 적용되지 않음

[원인]
멀티 DB 환경에선 각 DB별로 TransactionManager(매니저)가 생성되지만
어노테이션을 적용하며 매니저를 지정 하지 않을 경우 적용이 안되는것을 확인

[해결]
옵션 value를 사용해 매니저 지정

 @Transactional(value = "secondTransactionManager")
반응형
반응형

DTO 인터페이스

public interface DTO<T extends Domain, Y extends DTO> {
    Y getInstanceByEntity(T t);
    T toEntity();
}

DTO 구현체

@Getter
@Setter
public class NoticeDTO implements DTO<Notice, NoticeDTO> {

    private Long pkey;
    private String title;
    private String content;
    private Long createdDt = new Date().toInstant().getEpochSecond(); // Instant

    public NoticeDTO() {
    }

    public NoticeDTO(Notice notice) {
        this.pkey = notice.getPkey();
        this.title = notice.getTitle();
        this.content = notice.getContent();
        this.createdDt = notice.getCreatedDt().getEpochSecond();
    }

    @Override
    public NoticeDTO getInstanceByEntity(Notice notice) {
    	NoticeDTO noticeDTO = new NoticeDTO(notice);
        return noticeDTO;
    }


    @Override
    public Notice toEntity() {
        return Notice.builder()
            .pkey(this.pkey)
            .title(this.title)
            .content(this.content)
            .createdDt(Instant.ofEpochSecond(this.createdDt))
            .build();

    }

}

 

Entity 인터페이스 

public interface Domain {

}

Entity 구현체

@Entity
@Table(name = "Notice")
@Getter @ToString
public class Notice implements Domain {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long pkey;
    private String title;
    private String content;
    private Instant createdDt;

    public Notice() {
    }

    @Builder
    public Notice(Long pkey, String title, String content,
                  Instant createdDt) {
        this.pkey = pkey;
        this.title = title;
        this.content = content;
        this.createdDt = createdDt;
    }
}

 

Page<Entity> -> List<DTO> 변환 util

public class DataSourceUtils {

    private DataSourceUtils() {
    }

    public static <Y extends Domain, T extends DTO<Y, T>> List<T> convertEntityToDTOList(Page<Y> page, T t) {
        List<Y> content = page.getContent();
        return content.stream().map(x -> t.getInstanceByEntity(x)).collect(Collectors.toList());
    }

}
Page<Notice> page = noticeRepository.findAllByCode(code, pageable);
List<NoticeDTO> list = DataSurceUtils.convertEntityToDTOList(page, new NoticeDTO());
반응형
반응형


SPA를 사용하다보면 발생하는 새로고침 에러..
SPA는 모든 주소가 index.html을 향하게 돼있어 주소를 입력해 접속하거나, 새로고침을 하게되면
아래 첨부한 사진과 같이 404에러가 발생하게된다.

새로고침을 해도 해당 페이지로 이동을 하고싶다면 어떻게 해야될까?
방법은 간단하다. 앞서 말한 SPA의 특징인 모든 주소가 index.html을 향하게 된다는것을 이용하면 된다.
404 페이지는 error 페이지다. error가 발생하게 되면 index.html로 이동해주는 Controller를 만들면된다.
spring boot의 내장 인터페이스인 ErrorController를 상속해 에러발생시 index.html로 이동시켜줄 controller를 구현하겠다.

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;


@Controller
public class CustomErrorController implements ErrorController {

    private final String ERROR_PATH = "/error";

    @GetMapping(ERROR_PATH)
    public String redirectRoot(){
        return "index.html";
    }

    @Override
    public String getErrorPath(){
        return ERROR_PATH;
    }
}


오버라이드하는 getErrorPath의 경우 Spring 2.3.X 버전부터 사라졌다고 한다(https://stackoverflow.com/questions/62436379/how-to-replace-errorcontroller-deprecated-function-on-spring-boot)
getErrorPath를 제거하거나 하위호환을 위해 @Override 어노테이션을 제거한 후 null을 리턴하게해 사용하면 된다.

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;


@Controller
public class CustomErrorController implements ErrorController {

    private final String ERROR_PATH = "/error";

    @GetMapping(ERROR_PATH)
    public String redirectRoot(){
        return "index.html";
    }

    public String getErrorPath(){
        return null;
    }
}

반응형
반응형

설치 명령어

sudo apt install openjdk-8-jdk

설치 확인

java -version
반응형
반응형

 

@JsonInclude 어노데이션을 사용해 Response에 포함될 필드를 설정할 수 있다.

 

Include.ALWAYES.. 등과 함께 사용한다.

public static enum Include {
	ALWAYS,
	NON_NULL,
	NON_ABSENT,
	NON_EMPTY,
	NON_DEFAULT,
	CUSTOM,
	USE_DEFAULTS;

	private Include() {
	}
}

 

예시

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

@JsonInclude(Include.NON_NULL)
public class CategoryResponseModel {

    private final String type;
    private final String name;
    private final String icon;

    public CategoryResponseModel(CategoryDTO categoryDTO) {
        this.type = categoryDTO.getType();
        this.name = categoryDTO.getName();
        this.icon = categoryDTO.getIcon();
    }

    public String getType() {
        return type;
    }

    public String getName() {
        return name;
    }

    public String getIcon() {
        return icon;
    }
}

 

반응형

+ Recent posts