ã¼ãããã¯ãããSpring Bootã®ç°å¢æ§ç¯(with Visual Studio Code)
ç®æ¬¡
- æ§ç¯ã«ä½¿ç¨ããç°å¢
- 1. JDKã®ã¤ã³ã¹ãã¼ã«
- 2. Visual Studio Codeã®æ¡å¼µæ©è½ãã¤ã³ã¹ãã¼ã«
- 3. Spring Bootã®ããã¸ã§ã¯ããä½æ
- ä½è«ã§ããã»ã»ã»
æ§ç¯ã«ä½¿ç¨ããç°å¢
- ã¨ãã£ã¿ã¼ : Visual Studio Code
- OS : Widows 11
1. JDKã®ã¤ã³ã¹ãã¼ã«
以ä¸ã®çç±ãããRedHatã®OpenJDKãã¤ã³ã¹ãã¼ã«ãã¾ãã
- Oracleã®JDKã¯2018å¹´9æã®Java11ããæåã¨ãªã£ã¦ãã
- Visual Studio Codeã®Javaã®æ¡å¼µæ©è½ãRedHatçããµãã¼ããã¦ãã
â»ä»¥ä¸ã®ãµã¤ããåèã«ãã¦ã¤ã³ã¹ãã¼ã«ãã¾ããã Windowsã«OpenJDKãã¤ã³ã¹ãã¼ã«ããæé
1-1. OpenJDKããã¦ã³ãã¼ã
â»ãã¦ã³ãã¼ãããã«ã¯ãRedHatã®ã¢ã«ã¦ã³ããä½æããå¿ è¦ãããã¾ãã
1-2. ã¤ã³ã¹ãã¼ã©ã®å®è¡
RedHatã®ãµã¤ããããã¦ã³ãã¼ãããã¤ã³ã¹ãã¼ã©ãå®è¡ãã¾ãã ã¦ã£ã¶ã¼ãã§è¡¨ç¤ºãããã¡ãã»ã¼ã¸ã«å¾ã£ã¦ã¤ã³ã¹ãã¼ã«ãé²ãã¦ããã¾ãã
â»Java8以éã¯JAVA_HOMEã§ã¯ãªããã·ã¹ãã ç°å¢å¤æ°ã«ãã¹ãè¦ã«è¡ãã®ã§ãããã©ã«ãã®ã¾ã¾ã§OK
1-3. çµæã確èª
ã³ãã³ãããã³ãã(ãããã¯PowerShell)ã§ãjava -version
ãå®è¡ãã¦Javaã®ãã¼ã¸ã§ã³ã表示ãããã°ã¤ã³ã¹ãã¼ã«æåã§ãã
>java -version openjdk version "21.0.4" 2024-07-16 LTS OpenJDK Runtime Environment (Red_Hat-21.0.4.0+7-1) (build 21.0.4+7-LTS) OpenJDK 64-Bit Server VM (Red_Hat-21.0.4.0+7-1) (build 21.0.4+7-LTS, mixed mode, sharing) >
2. Visual Studio Codeã®æ¡å¼µæ©è½ãã¤ã³ã¹ãã¼ã«
æ¡å¼µæ©è½ã®ã¤ã³ã¹ãã¼ã«ã¯ä»¥ä¸ã®ãã¼ã¸ãåèã«å®æ½ãã¾ããã
â»3ã®æé ããä¸è¨2ãã¼ã¸ãåèã«ãã¦ãã¾ãã
2-1. Javaæ¡å¼µæ©è½ãã¤ã³ã¹ãã¼ã«
Extention Pack for Java
ãã¤ã³ã¹ãã¼ã«ã
â»VS Codeã®Javaã®æ¡å¼µæ©è½ã®All in Oneã¿ãããªããã¯ã§ãã
v0.29.0
ã®æ®µéã§ã¯ä»¥ä¸ã®æ¡å¼µæ©è½ãå
¥ã£ã¦ãã¾ãã
- Language Support for Java(TM) by RedHat
- Debugger for Java
- Test Runner for Java
- Maven for Java
- Gradle for Java
- Project Manager for Java
- IntelliCode
2-2. Spring Bootæ¡å¼µæ©è½ãã¤ã³ã¹ãã¼ã«
Spring Boot Extention Pack
ãã¤ã³ã¹ãã¼ã«ã
â»Spring Bootãæ±ãä¸ã§ä¾¿å©ãªæ¡å¼µæ©è½ã®å
¥ã£ãããã¯ã§ãã
v0.2.1
ã®æ®µéã§ã¯ä»¥ä¸ã®æ¡å¼µæ©è½ãå
¥ã£ã¦ãã¾ãã
3. Spring Bootã®ããã¸ã§ã¯ããä½æ
3-1. Javaããã¸ã§ã¯ããä½æ
Create Java Project
ãæ¼ä¸å¾ã以ä¸ãé çªã«è¨å®ãã¦ããã¾ãã
- project type(1)
Spring Boot
â»ããã§Spring Bootãé¸æããã¨ãSpring Initializr ã使ããã¨ã«ãªãããã§ãã Spring Initializr ã¯ããã«ããã¼ã«(ä»åã¯Maven)ãéçºè¨èª(ä»åã¯Java)ãé¸æããã¨èªåã§éå½¢ã¨ãªãã¢ããªã±ã¼ã·ã§ã³ãä½ã£ã¦ããããã®ã§ãã
- project type(2)
- Spring Bootã®ãã¼ã¸ã§ã³
3.3.3
â»SNAPSHOTçã§ã¯ãªãææ°ã®ãã®ãæå®ãã¾ããã
- 使ç¨ããè¨èª
Java
ä»ã«ãKotlinãªã©ãé¸æè¢ã«ããã¾ããããä»åã¯Javaãé¸æãã¾ããã
- Group Id
- ä»»æã®GroupIdã
- Artifact Id
- ä»»æã®ArtifactIdã
- packaging type
jar
war/jarã®é¸æã§ããããã£ãããjarãé¸æãã¾ããã
- Javaã®ãã¼ã¸ã§ã³
21
â»ããã¸ã§ã¯ãä½æå¾ããJAVA PROJECTS
>Configure Java Runtime
ãããç»åã®ããã«Project Settingsãéãã¦å¤æ´ã§ãã¾ãããäºåã«ã¤ã³ã¹ãã¼ã«ããJDKã®ãã¼ã¸ã§ã³ã«åããã¦ããã¾ããã Javaã®ãã¼ã¸ã§ã³ãå¤æ´ããå ´åã¯ãpom.xmlã®properties/java.version
ãåããã¦å¤æ´ããå¿ è¦ãããããã§ãã
- ä¾åããã©ã¤ãã©ãª
ä»»æã®ã©ã¤ãã©ãª â»ããã§æå®ããã©ã¤ãã©ãªãpom.xmlã«dependencyã¨ãã¦è¿½å ããã¾ãã pom.xmlã®ä»¥ä¸ã®é¨åã§ãã
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
ããã¸ã§ã¯ãã®ä½æã«æåããã¨ãç»åã®ãããªã¡ãã»ã¼ã¸ã表示ããã¾ãã
â»ç»åã¯ãã¹ãé»ãå¡ãã¤ã¶ãã¦ãã¾ãã
ä½æãããããã¸ã§ã¯ããéãã¨ãmainã¡ã½ãããä½æããã¦ãã¾ããã
3-2. å®è¡ç¢ºèª
èªåã§ä½æãããmainã¡ã½ãããå®è¡ããã¨ã以ä¸ã®ããã«åãã¾ããã
PS {projectã®ãã¹}> d:; cd '{projectã®ãã¹}'; & '{RedHatã®ãã¹}\java-21-openjdk-21.0.4.0.7-1\bin\java.exe' '@C:\Users\USER\AppData\Local\Temp\cp_4dj0i387059w3xhvviu0zhc0s.argfile' '{mainã¡ã½ããã®å®ç¾©ããã¦ããã¯ã©ã¹}' . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.3.3) 2024-09-03T21:13:18.952+09:00 INFO 16028 --- [{Artifact Id}] [ main] c.p.c.{ä½æããprojectã®Applicationã¯ã©ã¹} : Starting CaringDiaryApiApplication using Java 21.0.4 with PID 16028 ({Maven Projectã®ã¯ã©ã¹ãã¡ã¤ã«ã®ãã¹} started by USER in {projectã®ãã¹}) 2024-09-03T21:13:18.959+09:00 INFO 16028 --- [{Artifact Id}] [ main] c.p.c.{ä½æããprojectã®Applicationã¯ã©ã¹} : No active profile set, falling back to 1 default profile: "default" 2024-09-03T21:13:20.661+09:00 INFO 16028 --- [{Artifact Id}] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http) 2024-09-03T21:13:20.684+09:00 INFO 16028 --- [{Artifact Id}] [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2024-09-03T21:13:20.685+09:00 INFO 16028 --- [{Artifact Id}] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.28] 2024-09-03T21:13:20.802+09:00 INFO 16028 --- [{Artifact Id}] [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2024-09-03T21:13:20.804+09:00 INFO 16028 --- [{Artifact Id}] [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1780 ms 2024-09-03T21:13:21.369+09:00 INFO 16028 --- [{Artifact Id}] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/' 2024-09-03T21:13:21.384+09:00 INFO 16028 --- [{Artifact Id}] [ main] c.p.c.{ä½æããprojectã®Applicationã¯ã©ã¹} : Started CaringDiaryApiApplication in 3.029 seconds (process running for 3.486) 2024-09-03T21:14:17.012+09:00 INFO 16028 --- [{Artifact Id}] [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2024-09-03T21:14:17.013+09:00 INFO 16028 --- [{Artifact Id}] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2024-09-03T21:14:17.015+09:00 INFO 16028 --- [{Artifact Id}] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
â»ããã¸ã§ã¯ãä½ææã«JDBCãé¸æãã¦ããå ´åãããã©ã«ãã®ã¾ã¾ã§ã¯ã以ä¸ã®ã¨ã©ã¼ãçºçããèµ·åã§ããªãã®ã§æ³¨æãå¿ è¦ã§ãã
2024-09-03T21:25:34.266+09:00 WARN 6664 --- [{Artifact Id}] [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception with message: Failed to determine a suitable driver class (ä¸ç¥) *************************** APPLICATION FAILED TO START *************************** Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. 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 Reason: Failed to determine a suitable driver class Action: Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath. If you want an embedded database (H2, HSQL or Derby), please put it on the classpath. If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
èµ·åãããã«ã¯ãDBå¨ãã®å¦çãæ¸ããããã¾ã è¨å®ãå¿ è¦ããã§ãã ä»åã¯Spring Bootã§ãã®ãä½ãããã®ç°å¢æ§ç¯ãç®çãªã®ã§ããã£ããã以ä¸ã®ããã«ãpom.xmlã«å®ç¾©ããã¦ããJDBCã®dependencyãã³ã¡ã³ãã¢ã¦ããã¦ãç¡çããèµ·åããã¾ããã
<!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> -->
VS Codeã®ã¿ã¼ããã«ã« o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
ã¨ãã£ãæãã®ã¡ãã»ã¼ã¸ã表示ããããããã©ã¦ã¶ã§ç¢ºèªãã¦ã¿ã¾ãã
http://localhost:8080/
ããã©ã¦ã¶ã§ãããã¨ã以ä¸ã®ç»é¢ã表示ããã¾ããã
Whitelabel Error Page
ã¨ããã®ã¯ãControllerã¯ã©ã¹ãã¾ã ä½ããã¦ããªãããåºã¦ããããã§ãã
ãã ãSpringã®ç°å¢èªä½ã¯ããã¾ã§ããã°ãæ´ã£ãã¨ãããã¨ãªã®ã§ããã¨ã¯èªåã§ä½æãããéå½¢ããã¸ã§ã¯ããèä»ããã¦ããã°è¯ãããã§ãã
ä½è«ã§ããã»ã»ã»
ä¾åããã©ã¤ãã©ãªã«ãSpring Securityãå
¥ãã¦ããã¨ãWhitelabel Error Page
ã§ã¯ãªãããã°ã¤ã³ãã¼ã¸ã表示ãããããã§ãã
pom.xmlã®dependency
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
http://localhost:8080/
ã§è¡¨ç¤ºãããç»é¢