일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- Google Refund
- docker
- critical rendering path
- draganddrop
- OverTheWire
- Camera Movement
- Unity IAP
- Camera Zoom
- screencapture
- SDK upgrade
- Unity Editor
- server
- mongoDB
- nodejs
- --watch
- MySQL
- express
- Packet Network
- Spring Boot
- springboot
- unity
- rpg server
- Google Developer API
- react
- css framework
- Digital Ocean
- linux
- java
- spread 연산자
- Git
- Today
- Total
우당탕탕 개발일지
[SpringBoot] 1. spring boot 시작하기 본문
0. Java에 대한 이해
C언어는 운영체제별로 컴파일러가 따로 있다.
Java는 컴파일러가 한개고, 각 운영체제에 맞는 JVM이 각 운영체제가 이해할 수 있도록 재변환작업을 거친다. Java는 한번만 결과물을 만들어놓으면 운영체제에 상관없이 모두 동일한 결과를 만들 수 있다. 이 JVM은 인기가 많아서 다른 언어에서도 사용한다. java의 버전 = jdk 버전이다.
JDK > JRE > JVM
- java virtual machine : java 가상머신. 운영체제별로 각각 존재한다. 컴파일러가 만든 바이너리 코드를 읽고 검증하고 실행하는 역할을 한다
-java runtime environment : jvm + java program . 실행에 필요한 라이브러리 파일을 포함하고있다.
-Java Development Kit : 개발을 위한 도구가 들어있다. jre + 개발도구들. 컴파일러, 디버그 도구 등이 모두 포함되어있다.
1. 설치단계
java 설치
oracle의 java jdk를 다운받아보자. LTS 인 21.0.5 버전을 다운받았다.
https://www.oracle.com/kr/java/technologies/downloads/#jdk21-windows
Download the Latest Java LTS Free
Subscribe to Java SE and get the most comprehensive Java support available, with 24/7 global access to the experts.
www.oracle.com
jetbrains toolbox 및 intelliJ IDEA 설치
설치 후 켜서 intellJ IDEA community edition 을 설치해준다.
edition 설치 후 설정 > 구성 > 최대 힙 크기를 누르면 좀더 쾌적하게 사용할 수 있다. 2048로 설정해볼까?
postman 설치
홈페이지에서 다운받으면 된다. 테스트를 위해 필요하다.
mysql community 설치
https://dev.mysql.com/downloads/installer/
MySQL :: Download MySQL Installer
MySQL Installer 8.0.40 Note: MySQL 8.0 is the final series with MySQL Installer. As of MySQL 8.1, use a MySQL product's MSI or Zip archive for installation. MySQL Server 8.1 and higher also bundle MySQL Configurator, a tool that helps configure MySQL Serve
dev.mysql.com
프롬프트에서 다음명령어를 입력, 비밀번호 입력 및 정상적으로 접근되는지 확인한다.
mysql -u root -p
2. 프로젝트 시작하기
https://start.spring.io 에 접속
gradle-groovy , java 프로젝트. 이름등을 선택한다.
오른쪽은 의존성(dependency) 를 설정하는 부분임. 필요한 라이브러리나 프레임워크를 넣을 수 있다.(우선은 비운다)
Generate를 누르면 프로젝트 파일이 생기고, jetbrain에서 오픈한다.
라이브러리 추가하기
build.gradle에 다음을 추가한다. (rest API를 위해 필요한 라이브러리)
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
}
3. GET API
간단하게 두 숫자를 더하는 API를 만들어보자.
package com.group.library_app.controller.calculator;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CalculatorController {
@GetMapping("/add")
public int addTowNumbers(
@RequestParam int numA ,
@RequestParam int numB)
{
return numA+numB;
}
}
이것을 변수가 아닌 객체 형태로 받을 경우 다음처럼 간단히 변경된다.
package com.group.library_app.controller.calculator;
import com.group.library_app.dto.calculator.request.CalculatorAddRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CalculatorController {
@GetMapping("/add")
public int addTowNumbers(CalculatorAddRequest request)
{
return request.getNumA()+ request.getNumB();
}
}
CalculatorAddRequest 처럼 데이터를 전달하기 위한 객체를 DTO, Data Transfer Object 라고 한다.
dto는 dto 폴더에 모아 관리하는것이 일반적인 것 같다. CalculatorAddRequest 는 다음과같이 정의하며, 생성자가 필요하다.
package com.group.library_app.dto.calculator.request;
public class CalculatorAddRequest {
private final int numA;
private final int numB;
public CalculatorAddRequest(int numA, int numB) {
this.numA = numA;
this.numB = numB;
}
public int getNumB() {
return numB;
}
public int getNumA() {
return numA;
}
}
API 를 요청할 땐 postman에서 numA와 numB를 지정할땐 Params에 추가한다.
4. REST API
post에서는 http body를 사용한다. json을 사용한다( javascript object notation)
주의할 점은 파라미터 변수 앞에 @RequestBody 를 추가해야 정상적으로 인자로 변환된다.
@PostMapping("/multiply")
public int multiplyNumbers(@RequestBody CalculatorMultiplyRequest request){
return request.getNumA()* request.getNumB();
}
GET 에서는 인자 전달할때 Param을 사용했다면, POST API에서는 body에 json 형식으로 전달해야한다. 다음은 /multiply API의 body 예시이다.
{
"numA":10,
"numB":100
}
유저리스트 불러오기와 같이, 많은 데이터를 받아오고 싶을땐 json형식으로 응답을 받게 된다. 다음처럼 User라는 객체를 반환하고 싶은 경우 Getter가 존재해야만 정상적으로 반환된다.
@GetMapping("/user")
public User getUsers(){
return new User("aa",12);
}
package com.group.library_app.domain.user;
public class User {
private String name;
private Integer age;
public User(String name, Integer age) {
if(name == null || name.isBlank()){
throw new IllegalArgumentException("wrong name :" + name);
}
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
}
다음은 db를 연결해보도록 하자.
https://journal-devchop.tistory.com/57
[SpringBoot] MySQL 연결하기
Database 기본mysql 에 접속한다. 터미널을 열고,mysql -u root -p사용가능한 명령어들을 알아보자.아래 언어들은 DDL (Data Definition Language) 라고한다.create database [name]; ##데이터베이스 생성show databases; ##데
journal-devchop.tistory.com
'Server' 카테고리의 다른 글
[SpringBoot] 3. 리팩토링 & 스프링 컨테이너 (0) | 2025.01.05 |
---|---|
[SpringBoot] 2. MySQL 연결하기 (0) | 2025.01.04 |
[SpringBoot] Dependency requires at least JVM runtime version 17. This build uses a Java 11 JVM. (0) | 2025.01.02 |
[nestJs] digital ocean 에 웹클라이언트 & 서버 배포하기 (0) | 2024.12.25 |
[nodeJs] Express + MongoDB 웹서버 (2) (0) | 2024.05.12 |