본문 바로가기

Spring53

Server to Server의 연결 - header 값 지정하는 방법 URI에 Header 데이터를 추가하는 방법 // header값을 추가해 준다. RequestEntity requestEntity = RequestEntity .post(uri) .contentType(MediaType.APPLICATION_JSON) .header("x-authorization", "abcd") .header("custom-header", "fffff") .body(req); Header 값을 재활용하여 사용하는 방법 header는 고정이고 body 부분은 변경될 경우 예시 json 형태 { "header":{ "responseCode": "" }, "body":{ "name": "spring boot", "age": 130 } } Dto 생성 -> body부분은 제너릭으로 받아야 한다.. 2021. 6. 26.
Server to Server의 연결 - post URI 생성하는 방법: UriComponentsBuilder expend()를 이용해서 uri상의 데이터를 추가한다.( , 를 추가하여 파라미터를 계속해서 추가해 줄 수 있다.) // http://localhost:8081/api/server/user/{userId}/name/{userName} URI uri = UriComponentsBuilder .fromUriString("http://localhost:8081") .path("api/server/user/{userId}/name/{userName}") .encode() .build() // post 방식일 경우 PathVariable 값을 추가하는 방법: expand .expand(100, "steve") .toUri(); System.out.pr.. 2021. 6. 26.
Server to Server의 연결 - get client 입장에서 서버에 통신하기 위해서 임의로 URL 생성하는 방법: UriComponentsBuilder // 주소 만들기: http://localhost:8081/api/server/hello URI uri = UriComponentsBuilder .fromUriString("http://localhost:8081") .path("/api/server/hello") // 주소에 값을 넣는 방법: queryParam // http://localhost:8081/api/server/hello?name=steve&age=10 .queryParam("name", "steve") .queryParam("age", 10) .encode() .build() .toUri(); RestTemplate으로 get.. 2021. 6. 26.
Exception 처리 Web Application의 입장에서 바라보았을때, 에러가 났을 때 내려줄 수 있는 방법은 많지 않다. 1. 에러 페이지 2. 4XX Error or 5XX Error 3. Client가 200 외에 처리를 하지 못 할 때는 200을 내려주고 별도의 에러 Message 전달 @ControllerAdvice Global 예외 처리 및 특정 package / Controller 예외 처리 @ExceptionHandler 특정 Controller의 예외처리 전체 시스템에서 예외 처리를 하는 방법 dto 작성 package com.example.exception.dto; import javax.validation.constraints.Min; import javax.validation.constraints.N.. 2021. 6. 19.
Custom Validation 1. AssertTrue / False 와 같은 method 지정을 통해서 Custom Logic 적용 가능 // 입력받은 날짜가 올바른 형식인지 확인 함수 앞에 is라는 명칭을 붙여줘야 동작한다. @AssertTrue(message = "yyyyMM의 형식에 맞지 않습니다.") public boolean isreqYearMonthValidation(){ System.out.println("assert true call"); // LocalDate은 기본적으로 yyyyMMdd 형식이므로 받은 데이터에 강제적으로 DD값을 임의로 붙여준다. String bDate = getReqYearMonth() + "01"; try{ LocalDate localDate = LocalDate.parse(bDate, Dat.. 2021. 6. 19.
Validation Validation이란 프로그래밍에 있어서 가장 필요한 부분이다. 특히 java에서는 null값에 대해서 접근하려고 할 때 null pointer exception이 발생 함으로, 이러한 부분을 방지 하기 위해서 미리 검증을 하는 과정을 Validation이라고 한다. 단순 코드 예제 public void run(String account, String pw, int age){ if (account == null || pw == null){ return; } if (age == 0){ return; } // 정상 처리할 logic } 검증해야 할 값이 많은 경우 코드의 길이가 길어 진다. 구현에 따라서 달라 질 수 있지만 Service Logic과의 분리가 필요 하다. 흩어져 있는 경우 어디에서 검증을 .. 2021. 6. 19.