Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1주차 과제 - ToDo REST API 만들기 #115

Merged
merged 34 commits into from
Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
3377aec
feat: HttpServer를 사용하여 로컬 서버 테스트
jdalma Aug 1, 2022
93d75ff
feat: Task 모델 추가
jdalma Aug 1, 2022
aeb4fce
refactor: method , path 상수 선언
jdalma Aug 1, 2022
043cdc7
feat: ToDo REST 추가
jdalma Aug 2, 2022
bfe9b65
refactor: 응답 상태 enum 적용 및 import 세분화
jdalma Aug 3, 2022
151fffd
feat: Path 클래스 추가
jdalma Aug 3, 2022
a5966b1
refactor: 응답 코드 추가
jdalma Aug 3, 2022
aa69360
refactor: setter 제거 , 필드 별 final 키워드 추가
jdalma Aug 3, 2022
45637b5
feat: Path 경로변수 커스텀 예외 추가
jdalma Aug 3, 2022
2bb7c36
fix: Jackson은 빈 생성자가 필요하다
jdalma Aug 3, 2022
063d138
feat: TaskConverter 클래스 추가
jdalma Aug 3, 2022
735251f
feat: 응답 상태 반환
jdalma Aug 3, 2022
1b14a49
feat: Path 클래스 resourceEquals 메서드 추가
jdalma Aug 4, 2022
638df1c
feat: HttpMethod enum 추가
jdalma Aug 4, 2022
fc9cfd8
feat: Path Class - fullPath 문자열 필드 추가
jdalma Aug 4, 2022
7747125
refactor: ObjectMapper 제거
jdalma Aug 4, 2022
26b0673
docs: HttpMethod JavaDoc 작성
jdalma Aug 5, 2022
20dd100
docs: HttpResponse JavaDoc 작성
jdalma Aug 5, 2022
ef67b49
fix: HttpMethod DELETED → DELETE 수정
jdalma Aug 5, 2022
0b84d73
feat: ObjectMapper 제거
jdalma Aug 5, 2022
08efd59
refactor: 임시 try-catch 제거
jdalma Aug 5, 2022
ba33952
refactor: HttpMethod equals메서드 제거
jdalma Aug 5, 2022
992f82c
feat: Path resource null 체크 추가 및 메서드 추가
jdalma Aug 5, 2022
9a27b45
docs: TODO 주석 수정
jdalma Aug 5, 2022
787759a
fix: 응답 헤더 Content-Type 설정
jdalma Aug 5, 2022
0f49a5a
test: TaskConverter jsonToMap 메서드 테스트 코드
jdalma Aug 5, 2022
34b519d
refactor: hasPathVariable메소드 추가로 인해 커스텀 예외 제거
jdalma Aug 6, 2022
ab74c96
refactor: 풀이 영상을 통한 메소드 추출 작업
jdalma Aug 6, 2022
ddddde8
test: TaskConverter의 jsonToMap메소드 테스트 코드 추가
jdalma Aug 6, 2022
8cbddd8
refactor: TaskHandler 경로에 대한 Return Early
jdalma Aug 6, 2022
548b1ce
docs: @DisplayName 표시 내용 수정
jdalma Aug 6, 2022
e7af7e8
Revert "refactor: TaskHandler 경로에 대한 Return Early"
jdalma Aug 6, 2022
b4f1a46
refactor: TaskHandler 경로에 대한 Return Early
jdalma Aug 6, 2022
26b157d
refactor: Path 클래스 필드 private → public으로 변경
jdalma Aug 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/src/main/java/com/codesoom/assignment/App.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
package com.codesoom.assignment;

import com.codesoom.assignment.handler.TaskHandler;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import java.io.IOException;
import java.net.InetSocketAddress;

public class App {
public String getGreeting() {
return "Hello World!";
}

public static void main(String[] args) {
System.out.println(new App().getGreeting());

InetSocketAddress address = new InetSocketAddress(8000);

try {
HttpServer server = HttpServer.create(address , 0);
HttpHandler handler = new TaskHandler();
server.createContext("/" , handler);



server.start();

} catch (IOException e) {
e.printStackTrace();
}
}
}
27 changes: 27 additions & 0 deletions app/src/main/java/com/codesoom/assignment/handler/TaskHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.codesoom.assignment.handler;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

public class TaskHandler implements HttpHandler {

public final int SUCCESSFUL_OK = 200;
johngrib marked this conversation as resolved.
Show resolved Hide resolved
@Override
public void handle(HttpExchange exchange) throws IOException {
String method = exchange.getRequestMethod();
String path = exchange.getRequestURI().getPath();
jdalma marked this conversation as resolved.
Show resolved Hide resolved
System.out.println(String.format("[method] : %s , [path] : %s" , method , path));

String content = "Hello Codesoom!!!";

exchange.sendResponseHeaders(SUCCESSFUL_OK , content.getBytes().length);
OutputStream outputStream = exchange.getResponseBody();
outputStream.write(content.getBytes());
outputStream.flush();
outputStream.close();
}
}
22 changes: 22 additions & 0 deletions app/src/main/java/com/codesoom/assignment/models/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.codesoom.assignment.models;

public class Task {
private Long id;
private String title;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 클래스에서 setter를 제거하고 과제를 진행하는 것도 가능할지 생각해 봅시다.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setter를 제거하고 Task 생성자로만 통해서 값을 넣어보았습니다

Task task = taskConverter.convert(body);
Task newTask = new Task(id, task.getTitle());

Task를 2개 만들어서 처리했는데.. 이런 경우는 어떻게 처리하는게 좋을까요??

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newTask를 리턴해보세요.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋습니다. 이번 기회에 함께 공부할만한 건이 있습니다. "팩토리"에 대해 조사해 보세요. 이걸 공부할 땐 코드 예제보다 어떤 필요에서 비롯되었고, 어떤 개념을 표현하는 것인지에 집중해서 정리해 보도록 해보세요.

Copy link
Author

@jdalma jdalma Aug 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

팩토리에 대해 조사하다가 종립님의 글도 보게 되었네요 ㅎㅎ
도메인 주도 설계와 연관이 있는 것 같아 뭔가 거대한 문을 열게되는 느낌이네요
더 조사하고 정리하여 꼭 공유 드리도록 하겠습니다!!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

꼼꼼하게 공부하고 다양하게 실험해 보시길 바랍니다. 거대한 문을 열게 되는 느낌이라니 저까지 두근두근하는군요. 화이팅!


public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}