2021-06-26 TIL

김병호·2021년 6월 26일
0

fact

  • 서버 로그 설정하기
  • 모각코 참가

feeling

  • AWS를 이용해서 모니터링 설정을 했다. 애플리케이션과 서버에서 로그 설정을 끝내고, CloudWatch를 이용해서 모니터링 정보를 게재했다. 설정할때는 너무 힘들었는데, 실시간으로 서버상태를 확인하니 보람차다.
  • 사람들과 주간회고를 했다. 이전에 사람들에게 공부한 내용들이나 회고를 공유해주면 좋을 것 같다라고 말을 하긴했는데 사람들이 잘 따라줘서 고맙다. 서로의 성장을 돕는 모임이 되었으면 한다.

Findings

  • CloudWatch & Metric 정보 수집
  • Ngnix 로그 설정
  • Spring ResponseEntity 공부
    - Spring REST API를 개발하다보면 ResponseEntity를 사용하곤한다. 왜 이것을 쓰지라는 의문이 있었는데 오늘 Spring HTTP Request를 공부하다가 조금 알게 되었다.
    @PostMapping("request-body1")
    public void requestBody(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String body = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
        response.getWriter().write("hello");
    }

Body를 받을때 request를 받아 InputStream을 이용할 수 있다.

이를 개선하여 InputStream를 인자를 받아 사용할 수 있다.

  @PostMapping("request-body2")
    public void requestBody(InputStream inputStream, Writer responseWrite) throws IOException {
        String body = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
        responseWrite.write("hello");
    }

이를 개선해서 HTTP message를 스펙화한 HttpEntity를 사용한다.
body를 문자로 바꿔서 실행한다. 메시지 컨버터가 작동.

    @PostMapping("request-body3")
    public HttpEntity<String> requestBody(HttpEntity<String> httpEntity) throws IOException {
        String body = httpEntity.getBody();
        return new HttpEntity<>("hello");
    }

이것을 HttpEntity를 상속한 ResponseEntity를 통한 메시지와 상태 코드를 줄 수 있다.
HttpMessageConverter가 작동한다.

    @PostMapping("request-body3")
    public HttpEntity<String> requestBody(HttpEntity<String> httpEntity) throws IOException {
        String body = httpEntity.getBody();
        return new ResponseEntity<>("hello", HttpStatus.CREATED);
    }

@ResponseBody를 사용하여 Body를 바로 받을 수 있다.

  @ResponseBody
    @PostMapping("/request-body4")
    public HttpEntity<String> requestBody(@RequestBody String body) throws IOException {
        return new ResponseEntity<>("hello", HttpStatus.CREATED);
    }

Affirmation

  • 긍정적인 에너지를 갖자
profile
노력하는 개발자입니다!

0개의 댓글

관련 채용 정보