일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- netlify
- github
- 추상화
- AOP
- 메모이제이션
- dfs
- npm
- GOF
- 디자인 패턴
- azure
- 서브셋폰트
- process.env
- 객체지향
- 동적계획법
- CSS
- 클라우드
- mock
- DP
- package
- Solid
- MariaDB
- git
- 캡슐화
- 다형성
- Secret
- Java
- dotenv
- 상속
- bfs
- PostgreSQL
Archives
- Today
- Total
이것저것 해보기🌼
TO-DO LIST 만들기 프로젝트 2 (실행 및 테스트) 본문
<실행>
지난 시간에 실행을 위한 준비까지 마쳤으니, 이제 main method를 실행해보았다.
정상적으로 실행되면 아래와 같이 Tomcat started on port(s) : 8080 메세지가 뜨게 된다.
Postman에서 request를 날려보자.
아직 구현된것이 없으니 GET으로 날려도 반응은 없지만, READ ALL이 정상호출된것은 알수 있다.
<TodoController 보완>
이제 TodoController를 아래와 같이 세부 작성을 완료했다.
package org.example.controller;
import lombok.AllArgsConstructor;
import org.example.model.TodoEntity;
import org.example.model.TodoRequest;
import org.example.model.TodoResponse;
import org.example.service.TodoService;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;
@CrossOrigin
@AllArgsConstructor
@RequestMapping("/")
@RestController
public class TodoController {
private final TodoService service;
@PostMapping
public ResponseEntity<TodoResponse> create(@RequestBody TodoRequest request){
System.out.println("CREATE");
if(ObjectUtils.isEmpty(request.getTitle()))
return ResponseEntity.badRequest().build();
if(ObjectUtils.isEmpty(request.getOrder()))
request.setOrder(0L);
if(ObjectUtils.isEmpty(request.getCompleted()))
request.setCompleted(false);
TodoEntity result = this.service.add(request);
return ResponseEntity.ok(new TodoResponse(result));
}
@GetMapping("{id}")
public ResponseEntity<TodoResponse> readOne(@PathVariable Long id){
System.out.println("READ ONE");
TodoEntity result = this.service.searchById(id);
return ResponseEntity.ok(new TodoResponse(result));
}
@GetMapping
public ResponseEntity<List<TodoResponse>> readAll(){
System.out.println("READ ALL");
List<TodoEntity> list = this.service.searchAll();
List<TodoResponse> response = list.stream().map(TodoResponse::new)
.collect(Collectors.toList());
return ResponseEntity.ok(response);
}
@PatchMapping("{id}")
public ResponseEntity<TodoResponse> update(@PathVariable Long id, @RequestBody TodoRequest request){
System.out.println("UPDATE");
TodoEntity result = this.service.updateById(id, request);
return ResponseEntity.ok(new TodoResponse(result));
}
@DeleteMapping("{id}")
public ResponseEntity<?> deleteOne(@PathVariable Long id){
System.out.println("DELETE");
this.service.deleteById(id);
return ResponseEntity.ok().build();
}
@DeleteMapping
public ResponseEntity<?> deleteAll(){
System.out.println("DELETE ALL");
this.service.deleteAll();
return ResponseEntity.ok().build();
}
}
<Request 테스트>
이제 다시한번 RUN을 하고 POST Request를 보내보니, 응답 받는것을 볼수 있었다.
테스트1 과 테스트2를 POST로 보낸후 GET을 호출해보면 아래와 같이 응답이 온다.
[
{
"id": 1,
"title": "테스트",
"order": 0,
"completed": false,
"url": "http://localhost:8080/1"
},
{
"id": 2,
"title": "테스트2",
"order": 0,
"completed": false,
"url": "http://localhost:8080/2"
}
]
DELETE로 테스트1을 지워보면 응답이 200으로 정상작동되었다.
<테스트>
마지막으로 todo server에 대한 테스트를 수행해보고, 모두 구현된것이 확인했다면
이제 클라이언트를 붙일 준비는 완료가 된것이다.
클라이언트에서 내 TODO Server가 잘 작동하는지도 확인해보았다.
'BE > Spring Boot' 카테고리의 다른 글
[REST API] PUT 다루기 (0) | 2021.06.30 |
---|---|
[REST API] POST 다루기 (0) | 2021.06.30 |
[REST API] GET 다루기 (0) | 2021.06.30 |
웹 개발 개론 (0) | 2021.06.30 |
TO-DO LIST 만들기 프로젝트 1 (구현) (0) | 2021.06.28 |