# RESTFul Server 만들어보기
- 데이터베이스 없이 고정 데이터를 반환하는 Server를 작성해본다.
패키지 관리법
- 계층형패키지 vs 도메인형 패키지
- 프로젝트 규모가 작다면 어떤 방법을 사용해도 문제 없다.
- 프로젝트가 커진다면 도메인형 패키지를 고려하는 것이 좋다.
- 이 글에서는 도메인형으로 만들어볼 예정
패키지 추가
- lecture - controller, dto
- teacher - controller, dto
- 각 도메인 내부에 repository, service, entity 등의 패키지가 추가된다.
Lecture
LectureDto
package com.example.myschool.lecture.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public class LectureDto {
private int lectureId;
private int teacherId;
private String title;
private String descriptiton;
}
LectureController
package com.example.myschool.lecture.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.myschool.lecture.dto.LectureDto;
@RestController
public class LectureController {
private List<LectureDto> lectures = new ArrayList<LectureDto>();
public LectureController() {
lectures.add(new LectureDto(1, 1, "Java", "Java language Basic Course"));
lectures.add(new LectureDto(2, 2, "Spring boot", "Spring Boot with java"));
lectures.add(new LectureDto(3, 1, "Kotlin", "Kotlin language Basic Course"));
lectures.add(new LectureDto(4, 2, "Spring boot(kotlin)", "Spring Boot with kotlin"));
lectures.add(new LectureDto(5, 3, "Node.js", "Node.js + express"));
}
@GetMapping("/lectures")
public List<LectureDto> getLectures(@RequestParam(name="title", required=false) String title) {
if(title == null)
return lectures;
return lectures.stream()
.filter(l->l.getTitle().toUpperCase().contains(title.toUpperCase()))
.collect(Collectors.toList());
}
@GetMapping("/lectures/{id}")
public LectureDto getLecture(@PathVariable("id") int id) {
return lectures.stream()
.filter(l->l.getLectureId()==id)
.findFirst()
.orElse(null);
}
}
- /lectures 로 접속 시 모든 수업목록들이 응답이 된다.
- 만약 쿼리스트링 “?title=” 형태로 요청이 오면 수업 목록의 title 중에 포함된 요소를 보여준다.
- /lectures/{id} 는 PathVariable로 구성되어져 있다. 우리가 흔히 블로그 같은데 들어가면 볼 수 있는 형식이다 “https://blog.naver.com/tmp/123123” id는 LectureId와 동일한 값이 있으면 해당 객체를 반환한다.
Teacher
TeacherController
package com.example.myschool.teacher.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.myschool.teacher.dto.TeacherDto;
@RestController
public class TeacherController {
private List<TeacherDto> teachers = new ArrayList<TeacherDto>();
public TeacherController() {
teachers.add(new TeacherDto(1,"홍길동","Java/Kotlin Language Teacher"));
teachers.add(new TeacherDto(2,"홍길삼","Spring Framework Teacher"));
teachers.add(new TeacherDto(3,"박재웅","Node.js Teacher"));
}
@GetMapping("/teacher")
public List<TeacherDto> getTeachers(
@RequestParam(name="name", required=false)String name) {
if(name == null) {
return teachers;
}
return teachers
.stream()
.filter(t->t.getName().toUpperCase().contains(name.toUpperCase()))
.collect(Collectors.toList());
}
@GetMapping("/teacher/{id}")
public TeacherDto getTeacher(@PathVariable("id")int id) {
return teachers
.stream()
.filter(t->t.getTeacherId() == id)
.findFirst()
.orElse(null);
}
}
TeacherDto
package com.example.myschool.teacher.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public class TeacherDto {
private int teacherId;
private String name;
private String description;
}
# RESTFul Server 만들어보기
- 데이터베이스 없이 고정 데이터를 반환하는 Server를 작성해본다.
패키지 관리법
- 계층형패키지 vs 도메인형 패키지
- 프로젝트 규모가 작다면 어떤 방법을 사용해도 문제 없다.
- 프로젝트가 커진다면 도메인형 패키지를 고려하는 것이 좋다.
- 이 글에서는 도메인형으로 만들어볼 예정
패키지 추가
- lecture - controller, dto
- teacher - controller, dto
- 각 도메인 내부에 repository, service, entity 등의 패키지가 추가된다.
Lecture
LectureDto
package com.example.myschool.lecture.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public class LectureDto {
private int lectureId;
private int teacherId;
private String title;
private String descriptiton;
}
LectureController
package com.example.myschool.lecture.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.myschool.lecture.dto.LectureDto;
@RestController
public class LectureController {
private List<LectureDto> lectures = new ArrayList<LectureDto>();
public LectureController() {
lectures.add(new LectureDto(1, 1, "Java", "Java language Basic Course"));
lectures.add(new LectureDto(2, 2, "Spring boot", "Spring Boot with java"));
lectures.add(new LectureDto(3, 1, "Kotlin", "Kotlin language Basic Course"));
lectures.add(new LectureDto(4, 2, "Spring boot(kotlin)", "Spring Boot with kotlin"));
lectures.add(new LectureDto(5, 3, "Node.js", "Node.js + express"));
}
@GetMapping("/lectures")
public List<LectureDto> getLectures(@RequestParam(name="title", required=false) String title) {
if(title == null)
return lectures;
return lectures.stream()
.filter(l->l.getTitle().toUpperCase().contains(title.toUpperCase()))
.collect(Collectors.toList());
}
@GetMapping("/lectures/{id}")
public LectureDto getLecture(@PathVariable("id") int id) {
return lectures.stream()
.filter(l->l.getLectureId()==id)
.findFirst()
.orElse(null);
}
}
- /lectures 로 접속 시 모든 수업목록들이 응답이 된다.
- 만약 쿼리스트링 “?title=” 형태로 요청이 오면 수업 목록의 title 중에 포함된 요소를 보여준다.
- /lectures/{id} 는 PathVariable로 구성되어져 있다. 우리가 흔히 블로그 같은데 들어가면 볼 수 있는 형식이다 “https://blog.naver.com/tmp/123123” id는 LectureId와 동일한 값이 있으면 해당 객체를 반환한다.
Teacher
TeacherController
package com.example.myschool.teacher.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.myschool.teacher.dto.TeacherDto;
@RestController
public class TeacherController {
private List<TeacherDto> teachers = new ArrayList<TeacherDto>();
public TeacherController() {
teachers.add(new TeacherDto(1,"홍길동","Java/Kotlin Language Teacher"));
teachers.add(new TeacherDto(2,"홍길삼","Spring Framework Teacher"));
teachers.add(new TeacherDto(3,"박재웅","Node.js Teacher"));
}
@GetMapping("/teacher")
public List<TeacherDto> getTeachers(
@RequestParam(name="name", required=false)String name) {
if(name == null) {
return teachers;
}
return teachers
.stream()
.filter(t->t.getName().toUpperCase().contains(name.toUpperCase()))
.collect(Collectors.toList());
}
@GetMapping("/teacher/{id}")
public TeacherDto getTeacher(@PathVariable("id")int id) {
return teachers
.stream()
.filter(t->t.getTeacherId() == id)
.findFirst()
.orElse(null);
}
}
TeacherDto
package com.example.myschool.teacher.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public class TeacherDto {
private int teacherId;
private String name;
private String description;
}