Part 1. Spring Framework
Ch 03. 예제를 만들며 이해하는 스프링 웹 어플리케이션 핵심 기술
08. 데이터 밸리데이션 적용
- 일반적으로 새로운 Resource를 만들 때는 Post 메소드를 사용한다.
* 불필요한 import가 있거나 formatting이 마음에 들지 않으면 ctrl + alt + O를 누르면
불필요한 import와 format들이 삭제된다.
package com.fastcampus.programmin.dmaker.dto;
import com.fastcampus.programmin.dmaker.type.DeveloperLevel;
import com.fastcampus.programmin.dmaker.type.DeveloperSkillType;
import com.sun.istack.NotNull;
import lombok.*;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
public class CreateDeveloper {
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public static class Request{
@NotNull
private DeveloperLevel developerLevel;
@NotNull
private DeveloperSkillType developerSkillType;
@NotNull
@Min(0)
@Max(20)
private Integer experienceYears;
@NotNull
@Size(min = 3, max = 50, message = "memberId size must 3 ~ 50")
private String memberId;
@NotNull
@Size(min = 3, max = 20, message = "memberId size must 3 ~ 20")
private String name;
@Min(18)
private Integer age;
}
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public static class Response{
private DeveloperLevel developerLevel;
private DeveloperSkillType developerSkillType;
private Integer experienceYears;
private String memberId;
}
}
<CreateDeveloper.java>
package com.fastcampus.programmin.dmaker.controller;
import com.fastcampus.programmin.dmaker.dto.CreateDeveloper;
import com.fastcampus.programmin.dmaker.service.dMakerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@Slf4j
@RestController //사용자의 요청을 받아 json 형태로 응답을 반환 //ctrl + 클릭
@RequiredArgsConstructor
// DMakerController(Bean) DMakerService(Bean) DeveloperRepository(Bean)
//===========================Spring Application =============================
//
//
public class DMakerController {
private final dMakerService dMakerService;
@GetMapping("/developers") //사용자 요청이 /developers를 통해 들어온다면 (ex] GET /developers HTTP/1.1)
//
public List<String> getAllDevelopers() {
// GET /Developers HTTP/1.1
log.info("GET /developers HTTP/1.1");
return Arrays.asList("snow", "elsa", "Olaf");
}
@PostMapping("/create-developer")
public List<String> createDevelopers(
@Valid @RequestBody CreateDeveloper.Request request
)
{
log.info("request : {}", request);
dMakerService.createDeveloper(request);
return Collections.singletonList("Olaf");
}
}
<DMakerController.java>
----------------------------------------------------------------------------------------------------------------------------------
https://bit.ly/37BpXic
'[패스트캠퍼스] Spring' 카테고리의 다른 글
패스트캠퍼스 챌린지 24일차 (0) | 2022.02.16 |
---|---|
패스트캠퍼스 챌린지 23일차 (0) | 2022.02.15 |
패스트캠퍼스 챌린지 21일차 (0) | 2022.02.13 |
패스트캠퍼스 챌린지 20일차 (0) | 2022.02.12 |
패스트캠퍼스 챌린지 19일차 (0) | 2022.02.11 |
댓글