b

RFC7807 과 problem+json 본문

카테고리 없음

RFC7807 과 problem+json

dev.bistro 2017. 5. 2. 13:28


https://tools.ietf.org/html/rfc7807


기본적인 HTTP의 응답메시지에 들어있는 데이터로는 기본적인 내용만 파악가능할 뿐, 상세한 내용은 없다. 

관련한 스펙이다. 본문에서도 나와있는 것처럼 고객의 계좌에 충분한 잔고가 없을 경우의 응답은 403 Forbidden Code를 줄수 있을 것이다. 하지만 이것으로는 왜 금지된것인지, 해결방법은 무엇인지는 전적으로 클라이언트 코드에서 알아서 해야한다. 이를 위해 이 7807 스펙을 적용할 수 있을 것이다.


미디어 유형은 application/problem+json 이고, application/json 과 호환이 된다.


java의 구현체로는 링크 를 사용하면 되고, 스프링과의 사용은  problem-spring-web 을 추가하면 된다.


@GetMapping(value = "/{id}", produces =  APPLICATION_JSON_VALUE  )
    public ResponseEntity getProduct(@PathVariable  String id) {
        Account account = accountRepository.get(id);
        if (account == null || !account.enoughBalence() ){
            throw Problem.builder()
                         .withType(URI.create("https://example.org/order-failed"))
                         .withTitle("Order failed")
                         .withStatus(BAD_REQUEST)
                         .withCause(Problem.builder()
                                           .withType(URI.create("about:blank"))
                                           .withTitle("Out of Stock")
                                           .withStatus(BAD_REQUEST)
                                           .build())
                         .build();
        }
        ...
    }


서비스에서는 쓸 일이 없겠지만 stacktrace깢 보고자 할때는  둘 중 하나의 방법으로 셋팅을 하면 된다. https://github.com/zalando/problem-spring-web#stack-traces-and-causal-chains


Comments