|
1 | 1 | # 03: Java Migration from Python |
2 | 2 |
|
3 | | -## 시나리오 |
| 3 | +## Scenario |
4 | 4 |
|
5 | | -Contoso 아웃도어 컴파니의 마케팅 팀에서는 제품 홍보를 위한 마이크로 소셜 미디어 웹사이트를 빠르게 론칭하고 싶어 합니다. |
| 5 | +Contoso is a company that sells products for various outdoor activities. A marketing department of Contoso would like to launch a micro social media website to promote their products for existing and potential customers. |
6 | 6 |
|
7 | | -Python 개발자가 백엔드 API를 개발하고 있었습니다만, 개인적인 사정으로 인해 회사를 그만두게 되었습니다! 개발팀의 Java 개발자인 당신은 이 Python 백엔드 API를 Java 기반의 Spring Boot 앱으로 마이그레이션을 해야 합니다. |
| 7 | +Because a Python developer has left the company, the stakeholders asked to migrate the existing Python backend API app to Java, using Spring Boot. |
8 | 8 |
|
9 | | -## 사전 준비사항 |
| 9 | +Now, as a Java developer, you should migrate the existing FastAPI app to Spring Boot. You've got very little knowledge of Python and FastAPI, by the way. |
10 | 10 |
|
11 | | -[README](../README.md) 문서를 참조하여 개발 환경을 준비합니다. |
| 11 | +## Prerequisites |
12 | 12 |
|
13 | | -## 순서 |
| 13 | +Refer to the [README](../README.md) doc for preparation. |
14 | 14 |
|
15 | | -- [개발 과정 프롬프트](#개발-과정-프롬프트) |
16 | | - - [프론트엔드 앱 및 백엔드 앱 확인](#프론트엔드-앱-및-백엔드-앱-확인) |
17 | | - - [프론트엔드 앱 및 백엔드 앱 실행 확인](#프론트엔드-앱-및-백엔드-앱-실행-확인) |
18 | | - - [REST API 확인](#rest-api-확인) |
19 | | - - [Python 앱 로직 확인](#python-앱-로직-확인) |
20 | | - - [Spring Boot 앱으로 마이그레이션](#spring-boot-앱으로-마이그레이션) |
21 | | - - [Spring Boot 프로젝트 생성](#spring-boot-프로젝트-생성) |
22 | | - - [빌드 및 앱 구동](#빌드-및-앱-구동) |
23 | | - - [REST API 추가 및 확인](#rest-api-추가-및-확인) |
24 | | - - [Python에 작성된 REST API 추가](#python에-작성된-rest-api-추가) |
25 | | - - [Database URL 변경](#database-url-변경) |
26 | | - - [백엔드 앱 전환 및 확인](#백엔드-앱-전환-및-확인) |
27 | | - - [확인](#확인) |
| 15 | +## Getting Started |
28 | 16 |
|
29 | | -## 개발 과정 프롬프트 |
| 17 | +- [Check GitHub Copilot Agent Mode](#check-github-copilot-agent-mode) |
| 18 | +- [Prepare Custom Instructions](#prepare-custom-instructions) |
| 19 | +- [Prepare Spring Boot Project](#prepare-spring-boot-project) |
| 20 | +- [Migrate FastAPI API App](#migrate-fastapi-api-app) |
30 | 21 |
|
31 | | -Python 앱으로부터 Spring Boot 앱으로의 전체적인 마이그레이션 과정은 다음과 같습니다. |
| 22 | +### Check GitHub Copilot Agent Mode |
32 | 23 |
|
33 | | -* [프론트엔드 앱 및 백엔드 앱 확인](#프론트엔드-앱-및-백엔드-앱-확인) |
34 | | -* [Spring Boot 앱으로 마이그레이션](#spring-boot-앱으로-마이그레이션) |
35 | | -* [백엔드 앱 전환 및 확인](#백엔드-앱-전환-및-확인) |
| 24 | +1. Click the GitHub Copilot icon on the top of GitHub Codespace or VS Code and open GitHub Copilot window. |
36 | 25 |
|
37 | | -### 프론트엔드 앱 및 백엔드 앱 확인 |
| 26 | +  |
38 | 27 |
|
39 | | -#### 프론트엔드 앱 및 백엔드 앱 실행 확인 |
| 28 | +1. If you're asked to login or sign up, do it. It's free of charge. |
| 29 | +1. Make sure you're using GitHub Copilot Agent Mode. |
40 | 30 |
|
41 | | -이전 세션들에서 수행했던 Python 백엔드 앱 및 Node JS 프론트엔드 앱이 실행되어 있어야 합니다. 특히, Python 백엔드 앱을 기준으로 마이그레이션이 이루어지기 때문에 [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs)에 접속하여 해당 앱이 구동되어 있는지를 확인합니다. |
| 31 | +  |
42 | 32 |
|
43 | | -만일, 앱이 실행되어 있지 않다면, [Python 앱 가이드](../01-python.md)에 따라 앱을 구동해 주시기 바랍니다. |
| 33 | +1. Select model to either `GPT-4.1` or `Claude Sonnet 4`. |
44 | 34 |
|
45 | | -#### REST API 확인 |
| 35 | +### Prepare Custom Instructions |
46 | 36 |
|
47 | | -다음 URL을 호출하여 백엔드 앱에서 제공하고 있는 REST API들을 확인해 봅니다. 아래와 같이 12개의 REST API가 있는 것을 확인할 수 있습니다. |
| 37 | +1. Set the environment variable of `$REPOSITORY_ROOT`. |
48 | 38 |
|
49 | | -``` |
50 | | -http://127.0.0.1:8000/docs |
51 | | -``` |
| 39 | + ```bash |
| 40 | + # bash/zsh |
| 41 | + REPOSITORY_ROOT=$(git rev-parse --show-toplevel) |
| 42 | + ``` |
52 | 43 |
|
53 | | -#### Python 앱 로직 확인 |
| 44 | + ```powershell |
| 45 | + # PowerShell |
| 46 | + $REPOSITORY_ROOT = git rev-parse --show-toplevel |
| 47 | + ``` |
54 | 48 |
|
55 | | -Python 앱에 어떤 기능들이 포함되어 있는지 확인하기 위해 GitHub Copilot에 다음과 같이 질의합니다. 해당 기능을 확인하여 대략의 Python 로직을 이해하고 파일을 생성합니다. |
| 49 | +1. Copy custom instructions. |
56 | 50 |
|
57 | | -``` |
58 | | -이 파일의 소스 내용을 설명해 주고 다이어그램을 생성해줘 |
59 | | -``` |
| 51 | + ```bash |
| 52 | + # bash/zsh |
| 53 | + cp -r $REPOSITORY_ROOT/docs/custom-instructions/java/. \ |
| 54 | + $REPOSITORY_ROOT/.github/ |
| 55 | + ``` |
60 | 56 |
|
61 | | -다음 위치에 다이어그램 파일이 생성된 것을 확인합니다. |
| 57 | + ```powershell |
| 58 | + # PowerShell |
| 59 | + Copy-Item -Path $REPOSITORY_ROOT/docs/custom-instructions/java/* ` |
| 60 | + -Destination $REPOSITORY_ROOT/.github/ -Recurse -Force |
| 61 | + ``` |
62 | 62 |
|
63 | | -``` |
64 | | -.../python/diagram.md |
65 | | -``` |
| 63 | +### Prepare Spring Boot Project |
66 | 64 |
|
67 | | -### Spring Boot 앱으로 마이그레이션 |
| 65 | +1. Make sure that you're using GitHub Copilot Agent Mode with the model of `Claude Sonnet 4` or `GPT-4.1`. |
| 66 | +1. Install Spring Boot CLI. |
68 | 67 |
|
69 | | -이제 Spring Boot 앱으로 마이그레이션 할 준비가 되었습니다. 이제부터 GitHub Copilot을 이용하여 마이그레이션을 진행하겠습니다. 이후 질의과정은 예시일 뿐이므로, 본인의 상황에 맞게 변경하여 질의하며 마이그레이션을 진행해도 됩니다. |
| 68 | + ```bash |
| 69 | + sdk install springboot |
| 70 | + ``` |
70 | 71 |
|
71 | | -#### Spring Boot 프로젝트 생성 |
| 72 | +1. Use prompt like below to scaffold a Spring Boot app project. |
72 | 73 |
|
73 | | -Spring Boot Initializr를 이용하면 Spring Boot 앱을 쉽게 생성할 수 있습니다. Spring Initializr 익스텐션을 사용하여 VS Code에서 초기 프로젝트를 생성해 보도록 합니다. 설정 값은 다음과 같습니다. |
| 74 | + ```text |
| 75 | + I'd like to scaffold a Spring Boot app. Follow the instructions below. |
74 | 76 |
|
75 | | -* 프로젝트 선택: Create a Gradle Project |
76 | | -* Spring Boot Version: 3.4.4 |
77 | | -* Project language: Java |
78 | | -* Group Id: com.example |
79 | | -* Artifact Id: demo |
80 | | -* Package Name: com.example.demo |
81 | | -* Packaging type: Jar |
82 | | -* Java version: 21 |
83 | | -* Dependencies: |
84 | | - * Spring Web |
85 | | - * Spring Boot Actuator |
86 | | - * Lombok |
| 77 | + - Your working directory is `java`. |
| 78 | + - Identify all the steps first, which is you're going to do. |
| 79 | + - Use Spring Boot CLI to create the Spring Boot app project. |
| 80 | + - Use Gradle as the Java package manager. |
| 81 | + - Use the package name of `com.contoso.springapp`. |
| 82 | + - Use the artifact ID of `springapp`. |
| 83 | + - Use the group ID of `com.contoso`. |
| 84 | + - Use the package type of `jar`. |
| 85 | + - Use OpenJDK version of `21`. |
| 86 | + - Add dependencies - `Spring Web`, `Spring Boot Actuator` and `Lombok`. |
| 87 | + - Use the port number of `8080`. |
| 88 | + - Build the Spring Boot app and verify if the app is built properly. |
| 89 | + - Run this Spring Boot app and verify if the app is running properly. |
| 90 | + - If either building or running the app fails, analyze the issues and fix them. |
| 91 | + ``` |
87 | 92 |
|
88 | | -초기 Spring Boot 앱 프로젝트가 생성되었으면 우측 하단에의 "Open new project"를 클릭하여 방금 생성한 프로젝트를 오픈합니다. |
| 93 | +1. Click the `[keep]` button of GitHub Copilot to take the changes. |
89 | 94 |
|
90 | | - |
| 95 | +### Migrate FastAPI API App |
91 | 96 |
|
92 | | -#### 빌드 및 앱 구동 |
| 97 | +1. Make sure that you're using GitHub Copilot Agent Mode with the model of `Claude Sonnet 4` or `GPT-4.1`. |
| 98 | +1. Add [`product-requirements.md`](../product-requirements.md) and [`openapi.yaml`](../openapi.yaml) to GitHub Copilot. |
| 99 | +1. Use prompt like below to migrate FastAPI to Spring Boot. |
93 | 100 |
|
94 | | -최초 생성된 앱이 에러가 없는지 빌드 후 앱 구동을 통해 확인해 봅니다. |
| 101 | + ```text |
| 102 | + Now, we're migrating the existing FastAPI-based API app to Spring Boot API app. Carefully read the entire PRD and `openapi.yaml`. Follow the instructions below for the migration. |
| 103 | + |
| 104 | + - The existing FastAPI application is located at `python`. |
| 105 | + - Your working directory is `java/springapp`. |
| 106 | + - Identify all the steps first, which is you're going to do. |
| 107 | + - Analyze the application structure of the existing FastAPI app. |
| 108 | + - Migrate all the endpoints. Both corresponding endpoints should be exactly the same as each other. |
| 109 | + - Use SQLite as the database. |
| 110 | + - Use in-memory feature of SQLite. |
| 111 | + - The database should always be initialized whenever starting the app. |
| 112 | + - Use `openapi.yaml` that describes all the endpoints and data schema. |
| 113 | + - The API application should render Swagger UI page through a default endpoint. |
| 114 | + - The API application should render exactly the same OpenAPI document through a default endpoint. |
| 115 | + - DO NOT add anything not defined in `openapi.yaml`. |
| 116 | + - DO NOT modify anything defined in `openapi.yaml`. |
| 117 | + - If necessary, add more packages for OpenAPI and Swagger UI. |
| 118 | + ``` |
95 | 119 |
|
96 | | -``` |
97 | | -./gradlew clean build bootRun |
98 | | -``` |
| 120 | +1. Click the `[keep]` button of GitHub Copilot to take the changes. |
| 121 | +1. Once the application is built, verify if it's written properly or not. |
99 | 122 |
|
100 | | -터미널에 다음과 같은 로그가 출력되면 앱이 정상적으로 구동된 것입니다. |
| 123 | + ```text |
| 124 | + Run the Spring Boot app and verify if the app is properly running. Also verify the OpenAPI endpoint renders exactly the same content as `openapi.yaml`. |
101 | 125 |
|
102 | | - |
103 | | - |
104 | | -#### REST API 추가 및 확인 |
105 | | - |
106 | | -디펜던시가 에러 없이 잘 추가되었는지 확인하기 위해 RestController를 추가해 봅니다. 기본 호출할 REST API로 /hello 를 생성해 보겠습니다. GitHub Copilot을 Agent 모드로 변경하고, 모델을 "Claude 3.7 Sonnet"으로 변경한 후 다음과 같이 프롬프트를 입력합니다. |
107 | | - |
108 | | -``` |
109 | | -/hello REST API를 추가해주고, Swagger 설정도 추가해줘 |
110 | | -``` |
111 | | - |
112 | | -다시 한 번 앱을 빌드하고 실행시켜 봅니다. |
113 | | - |
114 | | -코드에 오류가 없으면 다음 URL을 이용하여 앱을 호출해 봅니다. |
115 | | - |
116 | | -``` |
117 | | -http://localhost:8080/hello |
118 | | -``` |
119 | | - |
120 | | -이번에는 Swagger UI를 접속해 봅니다. |
121 | | - |
122 | | -``` |
123 | | -http://localhost:8080/swagger-ui.html |
124 | | -``` |
125 | | - |
126 | | -앱이 정상적으로 호출되었으면 마이그레이션 준비가 완료된 것입니다. |
127 | | - |
128 | | -#### Python에 작성된 REST API 추가 |
129 | | - |
130 | | -Python 앱은 12개의 REST API로 이루어진 백엔드 서비스로 구성되어 있습니다. 이제 좀 전에 생성한 다이어그램 파일을 활용하여 이 REST API를 자바로 변경해 보겠습니다. |
131 | | - |
132 | | -GitHub Copilot에 다음과 같이 프롬프트를 입력합니다. |
133 | | - |
134 | | -``` |
135 | | -이 다이어그램 파일을 참고해서 동일한 API 주소를 갖는 함수들을 현재 프로젝트에 추가해줘. |
136 | | -``` |
137 | | - |
138 | | -앱을 빌드하고, Swagger UI에 접속해서 12개의 REST API가 추가되었는지를 확인합니다. 만일 빌드 시 에러가 발생하거나, 원하는 API 목록이 출력되지 않는다면 다음 프롬프트를 사용해서 정상적인 결과값이 나올때까지 수정을 반복해서 이슈를 해결합니다. |
139 | | - |
140 | | -``` |
141 | | -빌드 시 오류가 발생해. 오류를 해결해줘 |
142 | | -``` |
143 | | - |
144 | | -#### Database URL 변경 |
145 | | - |
146 | | -이제 기존 Python에서 사용하던 데이타베이스를 참조하도록 환경을 변경해 봅니다. 해당 파일은 다음 위치에 존재합니다. |
147 | | - |
148 | | -``` |
149 | | -/workspaces/github-copilot-bootcamp-2025/python/sns.db |
150 | | -``` |
151 | | - |
152 | | -Spring Boot 프로젝트의 다음 파일을 오픈해서 파일 주소를 변경합니다. |
153 | | - |
154 | | -* project_root: /workspaces/github-copilot-bootcamp-2025/java/demo |
155 | | -* 파일 명: <project_root>/src/main/resources/application.properties |
156 | | - |
157 | | -변경할 내용은 다음과 같습니다. |
158 | | - |
159 | | -``` |
160 | | -spring.datasource.url=jdbc:sqlite:sns.db |
161 | | -``` |
162 | | - |
163 | | -> **NOTE**: 만약 Python 앱에서 쓰던 `sns.db`를 그대로 활용하고 싶다면, `python/sns.db` 파일을 `java/demo/sns.db`로 복사합니다. |
164 | | -
|
165 | | -다시 앱을 빌드해서 데이타베이스가 제대로 참조되었는지 확인합니다. |
166 | | - |
167 | | -### 백엔드 앱 전환 및 확인 |
168 | | - |
169 | | -#### 확인 |
170 | | - |
171 | | -이제 모든 코드 생성이 완료되었습니다. 마지막으로 Node JS로 작성된 프론트엔드 앱에서 기존 URL과 동일하게 호출할 수 있도록 Spring Boot 앱의 포트를 변경합니다. |
172 | | - |
173 | | -변경 전 [여기](01-python.md#서비스-종료)를 참고하여 Python 앱을 구동 중지합니다. |
174 | | - |
175 | | - |
176 | | -포트 변경을 위한 설정을 변경하기 위해 GitHub Copilot에 다음과 같이 프롬프트를 입력합니다. |
177 | | - |
178 | | -``` |
179 | | -앱 서비스 포트를 8080으로 변경해줘 |
180 | | -``` |
181 | | - |
182 | | -이제 앱을 다시 빌드하고 구동시켜서 다음 URL로 접속이 되는지를 확인합니다. |
183 | | - |
184 | | -``` |
185 | | -https://localhost:8080/swagger-ui.html |
186 | | -``` |
187 | | - |
188 | | -앱이 정상적으로 구동되었으면 Node JS 프론트엔드 앱을 호출하여 앱에 이상이 없는지 확인합니다. |
189 | | - |
190 | | -``` |
191 | | -http://localhost:3000 |
192 | | -``` |
| 126 | + If app running fails, analyze the issues and fix them. |
| 127 | + ``` |
| 128 | +
|
| 129 | +1. Open a web browser and navigate to `http://localhost:8080`. |
| 130 | +1. Click the `[keep]` button of GitHub Copilot to take the changes. |
193 | 131 |
|
194 | 132 | --- |
195 | 133 |
|
196 | | -축하합니다! **Java 앱 개발** 실습이 끝났습니다. 이제 [STEP 04: .NET 앱 개발](./04-dotnet.md) 단계로 넘어가세요. |
| 134 | +OK. You've completed the "Java" step. Let's move onto [STEP 04: .NET Migration from JavaScript](./04-dotnet.md). |
0 commit comments