💐 Spring/Spring REST API

💐 Spring/Spring REST API

[HAEOAS] linkTo 메소드 exception 발생

중간 생략... POST 요청으로 보내면,, POST http://localhost:8080/boards/1/pockets requestBody : { "title" : "pocket title" } 아래 에러가 발생한다. 대충 보니까, PocketController에 addPocket 메소드에 linkTo를 실행하다가 에러가 난 것같다. boardId값을 확장하기 위한 변수값이 충분하지 않다..? 라고 뜨는데 뭔 말이지.. @PostMapping public ResponseEntity addPocket(@PathVariable Long boardId, @RequestBody PocketDto pocketDto) throws NotFoundException { System.out.println("boar..

💐 Spring/Spring REST API

7) [test] @JUnitParams 테스트 코드 리팩토링 - 스프링 REST API

비즈니스 로직 적용 됐는지 응답 메시지 확인 offline 과 free 값 확인 비즈니스 로직 일단 offline은 location에 값이 들어있으면 오프라인이고, 값이 없으면 온라인으로 진행이 된다. free값은 basePrice와, MaxPrice가 둘 다 0일때 , free 이다. Event.class 도메인 클래스에다가 비즈니스 로직을 적용시켜준다. public void update() { this.free = (basePrice == 0) && (maxPrice == 0); this.offline = location != null; } EventTests.class @Test public void testOffine() { // Given Event event = Event.builder() ...

💐 Spring/Spring REST API

6) [test] Bad_Request 응답 본문 만들기 - 스프링 REST API

이번에 할 것은 Bad Request 응답 본문 만들기 이다. EventController 코드를 살펴보면, @PostMapping public ResponseEntity createEvent(@RequestBody @Valid EventDto eventDto, Errors errors) { //@Valid 오류가 난다면, 의존성 추가 : spring-boot-starter-validation 을 해준다. if (errors.hasErrors()) { return ResponseEntity.badRequest().build(); } eventValidator.validate(eventDto, errors); if (errors.hasErrors()) { return ResponseEntity.badReq..

💐 Spring/Spring REST API

5) [test] 입력값 제한하기 (비즈니스 로직으로 검사) - 스프링 REST API

@Valid 와 BindingResult (또는 Errors) BindingResult 는 항상 @Valid 바로 다음 인자로 사용해야 함 (스프링 MVC) @NotNull, @NotEmpty, @Min, @Max ... 사용해서 입력값 바인딩 할때 에러 확인이 가능하다 도메인 Validator 만들기 Validator 인터페이스 없이 만들어도 상관없다. @Component public class EventValidator { public void validate(EventDto eventDto, Errors errors) { if (eventDto.getBasePrice() > eventDto.getMaxPrice() && eventDto.getMaxPrice() != 0) { errors.reject..

💐 Spring/Spring REST API

4) [test] 입력값 제한하기 (Bad_Request 발생) - 스프링 REST API

테스트 할 것 입력값으로 누가 id나 eventStatus, offline, free 이런 데이터까지 같이 주면? Bad_Request 발생 (이번 테스트!) vs 받기로 한 값 이외는 무시 핵심 코드 ObjectMapper 커스터마이징 spring.jackson.deserialization.fail-on-unknown-properties = true // deserialization : 역직렬화(JSON -> 객체) 를 하는데, unknown-properties가 있으면 -> fail-on 실패를 던진다.application.properties spring.jackson.deserialization.fail-on-unknown-properties=true #왜 deserialization이냐면, (Js..

💐 Spring/Spring REST API

3) [test] 입력값 제한하기 (무시하는 방법) - 스프링 REST API

테스트 할 것 입력값으로 누가 id나 eventStatus, offline, free 이런 데이터까지 같이 주면? Bad_Request 로 응답 vs 받기로 한 값 이외는 무시! (이번 테스트 방법) 입력값 제한 id 또는 입력 받은 데이터로 계산해야 하는 값들은 입력 받지 않아야 한다. EventDTO 적용 DTO -> 도메인 객체로 값 복사 ModelMapper 의존성 추가 org.modelmapper modelmapper 2.3.1 빈으로 등록 @Configuration 아래에 @Bean public ModelMapper modelMapper(){ return new ModelMapper(); }통합 테스트로 전환 @WebMvcTest 빼고 다음 애노테이션 추가 @SpringBootTest @Aut..

iseunghan
'💐 Spring/Spring REST API' 카테고리의 글 목록