01. 패키지와 스프링 애플리케이션 실행, postman(포스트맨) 설치하기
패키지를 만들고, 프로젝트를 실행해 보자.
이후 기초 구성과 데이터베이스 연결, 포스트맨 사용까지 진행해보려 한다.
※ 생성된 패키지 내 New → Package 를 선택해 새 패키지를 만든다.
springtistory 라는 이름으로 새 패키지를 생성했다.
메인 프로젝트, 클래스를 생성할 경우
보통은 <그룹 _이름.><프로젝트_이름> 형식으로 입력한다.
클래스를 생성한다.
클래스의 이름은 보통 <프로젝트_이름><Application> 형식으로 입력
※참고만 하고 실제 개발 상황에서는 내용을 고려하여 생성할 것.
나는 이미 스프링부트 제너레이터로 만들었기 때문에 해당 클래스가 있지만,
메인 클래스(진입점) 이 필요한 경우 생성해서 아래와 같이 입력한다.
SpringBootDeveloperApplication 이라는 이름으로 클래스를 생성.
예시)
package com.example.springbootdeveloper;
import org.springframework.boot.SpringApplication; //필요에 따라 import
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootDeveloperApplication {
SpringApplication.run(SpringBootDeveloperApplication.class, args);
}
}
// @SpringBootApplication 어노테이션, run 메서드
클래스가 import되지 않아 문제가 발생할 경우,
에러 위치에 커서를 두고
Alt + Enter 을 누르면 패키지를 import 할 수 있다.
스프링 애플리케이션 실행하기
아까 데이터베이스 설정을 주석처리하지 않은 탓에 아래와 같은 오류가 나서
다시 주석처리 후 실행해 보았다.
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
이전 게시글에서 말한 의존성에 JPA와 mysql 두개 다 주석 처리하면 된다.
스프링부트는 JPA 설정을 자동으로 시작하면서 , DB연결도 자동 구성하려고 하기 때문에
내가 application.properties에 DB 접속 정보가 없는 상황에서는 DB관련 정보만이 아닌
JPA 의존성도 주석처리해주어야 한다.
문제 없이 실행됨.
메세지를 보면,
1. 스프링부트 애플리케이션이 시작되고
2. 내장 서버인 Tomcat이 8080포트에서 초기화된다. 이는 웹 브라우저에서 http://localhost:8080 으로 접속할 수 있다는 의미이다.
3. 스프링 애플리케이션 컨텍스트 로딩이 완료되고, 톰캣이 루트 경로에서 실행되고 있음을 의미한다.
C:\Users\write\.jdks\jbr-17.0.9\bin\java.exe -XX:TieredStopAtLevel=1 -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-Dmanagement.endpoints.jmx.exposure.include=*" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2023.3.6\lib\idea_rt.jar=50324:C:\Program Files\JetBrains\IntelliJ IDEA 2023.3.6\bin" -Dfile.encoding=UTF-8 -classpath C:\TAVEspringbootOne\springboot-developer\build\classes\java\main;C:\TAVEspringbootOne\springboot-developer\build\resources\main;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.projectlombok\lombok\1.18.36\5a30490a6e14977d97d9c73c924c1f1b5311ea95\lombok-1.18.36.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-web\3.4.4\441c0c71f38783c1d34b8bac5f06f1fa6b103274\spring-boot-starter-web-3.4.4.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-json\3.4.4\7f7ca72abcab50661b7b5d23f63b3bc2375bb2b3\spring-boot-starter-json-3.4.4.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter\3.4.4\6ad00ebe69a28a5c1c97f80f81920d65e0e4250b\spring-boot-starter-3.4.4.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-tomcat\3.4.4\4b18ac49cf13ef2c9a98418ebd8fc212a5259da9\spring-boot-starter-tomcat-3.4.4.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.springframework\spring-webmvc\6.2.5\db8f1171041d7091f3de80cffdfb9d6c5fbf3015\spring-webmvc-6.2.5.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.springframework\spring-web\6.2.5\b42d2c0acbe05bad4c849883aa8816c25b6c1d94\spring-web-6.2.5.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.datatype\jackson-datatype-jsr310\2.18.3\cc57924cccf42fc852081c36215272f84ffcd991\jackson-datatype-jsr310-2.18.3.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.module\jackson-module-parameter-names\2.18.3\fa63ae5eb3956b1a6d7a7ead2b513af89dea22e7\jackson-module-parameter-names-2.18.3.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.datatype\jackson-datatype-jdk8\2.18.3\621558295660935134b171ce2b0d9ad6842ec2ff\jackson-datatype-jdk8-2.18.3.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-databind\2.18.3\537e3886263e3b3464385040453e92567fd509e2\jackson-databind-2.18.3.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-autoconfigure\3.4.4\5ebc8c0682374768ee6eac9acf12f41e76762207\spring-boot-autoconfigure-3.4.4.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot\3.4.4\f9dbe14c2e5e35a2cd27156802ea6b7c42ab34fd\spring-boot-3.4.4.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-logging\3.4.4\1a7bbfd57e0b2d0bde8ffd6aa7a0ebd94e4b0aed\spring-boot-starter-logging-3.4.4.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\jakarta.annotation\jakarta.annotation-api\2.1.1\48b9bda22b091b1f48b13af03fe36db3be6e1ae3\jakarta.annotation-api-2.1.1.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.springframework\spring-core\6.2.5\d6786db7122037bf605e54e3b35f262a19b8d502\spring-core-6.2.5.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.yaml\snakeyaml\2.3\936b36210e27320f920536f695cf1af210c44586\snakeyaml-2.3.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-websocket\10.1.39\abf50abbbae651f95dfcf9fae05f919bfa9b7046\tomcat-embed-websocket-10.1.39.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-core\10.1.39\f6acead04214d5aaea82c2639392208df33b3abe\tomcat-embed-core-10.1.39.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-el\10.1.39\88ff05c768c4654097ab0a9f2b49368a0877ad76\tomcat-embed-el-10.1.39.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.springframework\spring-context\6.2.5\237de0c3afca2099ab497cc7464726c02b8ab5c5\spring-context-6.2.5.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.springframework\spring-aop\6.2.5\9f436be65bf45ee8643d93e6823d6c81e8f9f91a\spring-aop-6.2.5.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.springframework\spring-beans\6.2.5\6f8eb671d9905da5a73bc9ab7e703e9fed6a0c3f\spring-beans-6.2.5.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.springframework\spring-expression\6.2.5\cb39181911dabe3b3d9992c4b4da38468726ea72\spring-expression-6.2.5.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\io.micrometer\micrometer-observation\1.14.5\b23dff6bf07a29f67fdae8f3f3f8f1c78fa7b126\micrometer-observation-1.14.5.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-annotations\2.18.3\7fa21cf7da4598f8240e4ebd9779249622af1acd\jackson-annotations-2.18.3.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-core\2.18.3\78f80c259268200e588aa204dd97ecf09b76916e\jackson-core-2.18.3.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-classic\1.5.18\fc371f3fc97a639de2d67947cffb7518ec5e3d40\logback-classic-1.5.18.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.apache.logging.log4j\log4j-to-slf4j\2.24.3\da1143e2a2531ee1c2d90baa98eb50a28a39d5a7\log4j-to-slf4j-2.24.3.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.slf4j\jul-to-slf4j\2.0.17\524cb6ccc2b68a57604750e1ab8b13b5a786a6aa\jul-to-slf4j-2.0.17.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.springframework\spring-jcl\6.2.5\1b8db3715dc78bd18b60869458d5ee8829ad8e99\spring-jcl-6.2.5.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\io.micrometer\micrometer-commons\1.14.5\6201a40489ccedc9539c5f7a2c84e9e64702bf10\micrometer-commons-1.14.5.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-core\1.5.18\6c0375624f6f36b4e089e2488ba21334a11ef13f\logback-core-1.5.18.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.slf4j\slf4j-api\2.0.17\d9e58ac9c7779ba3bf8142aff6c830617a7fe60f\slf4j-api-2.0.17.jar;C:\Users\write\.gradle\caches\modules-2\files-2.1\org.apache.logging.log4j\log4j-api\2.24.3\b02c125db8b6d295adf72ae6e71af5d83bce2370\log4j-api-2.24.3.jar com.example.springbootdeveloper.SpringbootDeveloperApplication
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.4.4)
2025-03-30T21:31:37.451+09:00 INFO 31712 --- [springboot-developer] [ main] c.e.s.SpringbootDeveloperApplication : Starting SpringbootDeveloperApplication using Java 17.0.9 with PID 31712 (C:\TAVEspringbootOne\springboot-developer\build\classes\java\main started by write in C:\TAVEspringbootOne\springboot-developer)
2025-03-30T21:31:37.454+09:00 INFO 31712 --- [springboot-developer] [ main] c.e.s.SpringbootDeveloperApplication : No active profile set, falling back to 1 default profile: "default"
2025-03-30T21:31:38.364+09:00 INFO 31712 --- [springboot-developer] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
2025-03-30T21:31:38.381+09:00 INFO 31712 --- [springboot-developer] [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2025-03-30T21:31:38.381+09:00 INFO 31712 --- [springboot-developer] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.39]
2025-03-30T21:31:38.453+09:00 INFO 31712 --- [springboot-developer] [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2025-03-30T21:31:38.454+09:00 INFO 31712 --- [springboot-developer] [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 954 ms
2025-03-30T21:31:38.896+09:00 INFO 31712 --- [springboot-developer] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/'
2025-03-30T21:31:38.903+09:00 INFO 31712 --- [springboot-developer] [ main] c.e.s.SpringbootDeveloperApplication : Started SpringbootDeveloperApplication in 1.95 seconds (process running for 2.66)
2025-03-30T21:35:41.298+09:00 INFO 31712 --- [springboot-developer] [ionShutdownHook] o.s.b.w.e.tomcat.GracefulShutdown : Commencing graceful shutdown. Waiting for active requests to complete
2025-03-30T21:35:41.863+09:00 INFO 31712 --- [springboot-developer] [tomcat-shutdown] o.s.b.w.e.tomcat.GracefulShutdown : Graceful shutdown complete
Process finished with exit code 130
실행 중 localhost:8080 에 접속 시, 404 오류가 나오는 페이지가 제대로 실행된다.
이는 정상적으로 서버가 실행된 것이며,
실행되지 않은 경우에는 아래와 같이 사이트에 연결할 수 없다고 나온다.
whitelable Error Page 의 404 오류는
요청은 잘 수행되었으나, 요청에 해당되는 페이지가 없어서 나오는 것이다.
우리가 localhost:8080 을 요청하면 index.html 을 찾도록 설정되어 있는데, index.html 파일을 프로젝트에 추가하지 않았기 때문이다.
resource 에 index.html 파일을 추가하여 다시 서버를 실행해 보자.
● index.html
<! DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<title></title>
</head>
<body>
<p>index.html</p>
</body>
</html>
index.html의 내용이 잘 나온다.
무언가를 따로 설정하지 않았는데 왜 index.html 파일을 인식하냐고 물어본다면
Springboot가 기본적으로 src/main/resources/static/index.html 을 루트 경로로 ( / ) 자동 매핑하고 있기 때문이다.
Springboot 의 정적 리소스 처리 기본 규칙에서
classpath:/static/
classpath:/public/
classpath:/resources/
등의 위치의 정적 파일들을 자동으로 서빙하도록 되어 있기 때문이다.
※ 공식 문서의
If you put an index.html file in any of the configured static locations,
it will be served as the default welcome page at /.
예시로 넣어 본 welcome page.
포스트맨 설치하기
백엔드에서 API 를 만들고 테스트를 하기 위해서는, 포스트맨 같은 클라이언트 프로그램이 필요하다.
spring에서는 API 문서화와 테스트를 위해 swagger을 사용해도 되지만, 더 세세한 테스트가 필요하다면 postman을 사용할 수 있다.
API
포스트맨은 HTTP 요청을 보낼 수 있는 클라이언트 프로그램이다.
우리는 API, 사용자와 서버가 통신하기 위한 인터페이스를 만들게 되는데, 몇 번의 클릭으로
API 테스트를 간편하게 할 수 있다.
swaager은 설치가 필요 없지만, postman은 설치가 필요하므로 설치 과정부터 진행하겠다.
1. 포스트맨 사이트에 접속하여 운영체제에 맞게 다운로드한다.
Download Postman | Get Started for Free
Download Postman | Get Started for Free
Try Postman for free! Join 35 million developers who rely on Postman, the collaboration platform for API development. Create better APIs—faster.
www.postman.com
다시 이어서 작성
※ 참고
포스트맨(Postman) 사용법과 API 실행해 보기
포스트맨의 사용방법과 이를 이용하여 API를 실행하여 테스트 하는 방법에 대해 소개한다. API에 요청을 보내고 결과를 확인하고 싶을 때, 브라우저에 직접 API URL을 작성하여 결과를 확인할 수 있
binit.tistory.com