반응형


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;
    }
}

 

반응형
반응형
반응형
반응형

@JsonIgnore: Response에 해당 필드가 제외된다
@JsonProperty: Response에 해당 속성의 이름이 변경된다

@Getter
@Setter
public class ResponseModel {

  private String data;
  private boolean result;
  @JsonProperty("errorMessage")
  private String errorMsg;
  @JsonIgnore
  private Integer errorCode;

}

 

RESPONSE

{
  "data":"response data------",
  "result":false,
  "errorMessage":"errorrrrr"
}

 

반응형
반응형
String contextPath = ServletUriComponentsBuilder.fromCurrentContextPath().toUriString();
반응형

+ Recent posts