【Java】编辑工具IntelliJ IDEA Community Edition写SpringBoot的HelloWorld
通过IDEA Community版本编辑器写SpringBoot的HelloWorld
前言
在开始之前,需要了解下
IntelliJ IDEA Ultimate(旗舰版)
和IntelliJ IDEA Community Edition(社区版)
的区别
费用
IntelliJ IDEA Ultimate(旗舰版):
收费
IntelliJ IDEA Community Edition(社区版):
免费
开发
IntelliJ IDEA Ultimate(旗舰版):
类似于全自动步枪,异常流畅,很智能
IntelliJ IDEA Community Edition(社区版):
类似于半自动步枪,异常流畅,对比Ultimate
略显弱智
Spring Boot Hello Word
1.创建
点击菜单
选择File>New>Project...
弹出新窗体
输入名称:SpringBootHelloWord
设置项目路径:G:\1CodeTest\Java
编译模式:Maven
JDK版本:1.8
点击Create
完成创建
会有个询问,我这里选择新的窗体,NewWindow
显示项目窗体
项目结构大致如下
2.新建 Controller和HelloWorld
在src》java右键》New》Java Class
输入类名 SpingBoot.Hello.Controller.HelloWorld
后,按键盘 Enter
完成新建
此时的项目目录结构如下图:
src》 main》 java 》SpingBoot.Hello.Controller》HelloWorld
打开HelloWorld.java
,代码如下
package Controller;
@RestController
public class HelloWorld {
@RequestMapping("/hello") //路由指向
public String hello(){
LocalDateTime now=LocalDateTime.now();
return "Spring boot Hello World !当前时间:"+ now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
}
如图
上图中,代码里的@RestController
和@RequestMapping
是红色的,需要我们手动引入
我们需要把光标放到@RestController
上会出现个小灯泡,
点击小灯泡,会出现步骤3的窗体,
点击Add Maven dependency...
出现下图窗体
点击Search For Artfact
在输入框输入org.springframework.boot
找到org.springframework.boot:spring-boot-starter-web:2.6.13
点击Add
完成添加引用
此时,你项目的pom.xml
文件会出现下面代码
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.13</version>
</dependency>
这个就是我们刚才引入的包
回到HelloWorld,java
文件
此时@RestController
和@RequestMapping
依旧是红色的
但是,当我们鼠标移动到@RestController
上面后提示 import class...
,点击它
点击后他会生成如下代码
import org.springframework.web.bind.annotation.RestController;
同样
我们鼠标移动到@RequestMapping
上面后提示 import class...
,点击它
import org.springframework.web.bind.annotation.RequestMapping;
此时,在IDEA里面,@RestController
和@RequestMapping
会变成黄色,
如图
此时完整代码如下
package SpingBoot.Hello.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@RestController
public class HelloWorld {
@RequestMapping("/hello")
public String hello(){
LocalDateTime now=LocalDateTime.now();
return "JJYCheng Spring boot Hello World !当前时间:"+ now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
}
如下图
3.新建引导类
在src》java右键》New》Java Class
类名输入SpingBoot.Hello.HelloApplication
后,按键盘 Enter
完成新建
此时项目结构如下图
编写代码如下
/**
* 引导类-SpringBoot项目的入口
* **/
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args){
SpringApplication.run(HelloApplication.class,args);
}
}
如下图
@SpringBootApplication
和SpringApplication
是红色的,说明没有引入,
我们鼠标移动到上面,然后,点击import class...
处理后的完整代码如下
package SpingBoot.Hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 引导类-SpringBoot项目的入口
* **/
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args){
SpringApplication.run(HelloApplication.class,args);
}
}
如下图
4.运行
打开 HelloApplication.java
文件,
点击左边三角运行按钮
或者在空白区域右键
然后点击Run HelloApplication.main()
运行成功后,右上角显示效果是这样的,如下图:
控制台显示如下图:
控制台文本
"C:\Program Files\Java\jdk1.8.0_261\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.1.4\lib\idea_rt.jar=14932:D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.1.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_261\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\rt.jar;G:\1CodeTest\Java\SpringBootHelloWord\target\classes;G:\1CodeTest\Java\Maven\Repository\org\springframework\boot\spring-boot-starter-web\2.6.13\spring-boot-starter-web-2.6.13.jar;G:\1CodeTest\Java\Maven\Repository\org\springframework\boot\spring-boot-starter\2.6.13\spring-boot-starter-2.6.13.jar;G:\1CodeTest\Java\Maven\Repository\org\springframework\boot\spring-boot\2.6.13\spring-boot-2.6.13.jar;G:\1CodeTest\Java\Maven\Repository\org\springframework\boot\spring-boot-autoconfigure\2.6.13\spring-boot-autoconfigure-2.6.13.jar;G:\1CodeTest\Java\Maven\Repository\org\springframework\boot\spring-boot-starter-logging\2.6.13\spring-boot-starter-logging-2.6.13.jar;G:\1CodeTest\Java\Maven\Repository\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;G:\1CodeTest\Java\Maven\Repository\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;G:\1CodeTest\Java\Maven\Repository\org\slf4j\slf4j-api\1.7.32\slf4j-api-1.7.32.jar;G:\1CodeTest\Java\Maven\Repository\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;G:\1CodeTest\Java\Maven\Repository\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;G:\1CodeTest\Java\Maven\Repository\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;G:\1CodeTest\Java\Maven\Repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;G:\1CodeTest\Java\Maven\Repository\org\springframework\spring-core\5.3.23\spring-core-5.3.23.jar;G:\1CodeTest\Java\Maven\Repository\org\springframework\spring-jcl\5.3.23\spring-jcl-5.3.23.jar;G:\1CodeTest\Java\Maven\Repository\org\yaml\snakeyaml\1.29\snakeyaml-1.29.jar;G:\1CodeTest\Java\Maven\Repository\org\springframework\boot\spring-boot-starter-json\2.6.13\spring-boot-starter-json-2.6.13.jar;G:\1CodeTest\Java\Maven\Repository\com\fasterxml\jackson\core\jackson-databind\2.13.4.2\jackson-databind-2.13.4.2.jar;G:\1CodeTest\Java\Maven\Repository\com\fasterxml\jackson\core\jackson-annotations\2.13.4\jackson-annotations-2.13.4.jar;G:\1CodeTest\Java\Maven\Repository\com\fasterxml\jackson\core\jackson-core\2.13.4\jackson-core-2.13.4.jar;G:\1CodeTest\Java\Maven\Repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.4\jackson-datatype-jdk8-2.13.4.jar;G:\1CodeTest\Java\Maven\Repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.4\jackson-datatype-jsr310-2.13.4.jar;G:\1CodeTest\Java\Maven\Repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.4\jackson-module-parameter-names-2.13.4.jar;G:\1CodeTest\Java\Maven\Repository\org\springframework\boot\spring-boot-starter-tomcat\2.6.13\spring-boot-starter-tomcat-2.6.13.jar;G:\1CodeTest\Java\Maven\Repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.68\tomcat-embed-core-9.0.68.jar;G:\1CodeTest\Java\Maven\Repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.68\tomcat-embed-el-9.0.68.jar;G:\1CodeTest\Java\Maven\Repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.68\tomcat-embed-websocket-9.0.68.jar;G:\1CodeTest\Java\Maven\Repository\org\springframework\spring-web\5.3.23\spring-web-5.3.23.jar;G:\1CodeTest\Java\Maven\Repository\org\springframework\spring-beans\5.3.23\spring-beans-5.3.23.jar;G:\1CodeTest\Java\Maven\Repository\org\springframework\spring-webmvc\5.3.23\spring-webmvc-5.3.23.jar;G:\1CodeTest\Java\Maven\Repository\org\springframework\spring-aop\5.3.23\spring-aop-5.3.23.jar;G:\1CodeTest\Java\Maven\Repository\org\springframework\spring-context\5.3.23\spring-context-5.3.23.jar;G:\1CodeTest\Java\Maven\Repository\org\springframework\spring-expression\5.3.23\spring-expression-5.3.23.jar" SpingBoot.Hello.HelloApplication
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.13)
2024-08-04 11:31:51.208 INFO 10236 --- [ main] SpingBoot.Hello.HelloApplication : Starting HelloApplication using Java 1.8.0_261 on DESKTOP-2T12I06 with PID 10236 (G:\1CodeTest\Java\SpringBootHelloWord\target\classes started by cheng in G:\1CodeTest\Java\SpringBootHelloWord)
2024-08-04 11:31:51.213 INFO 10236 --- [ main] SpingBoot.Hello.HelloApplication : No active profile set, falling back to 1 default profile: "default"
2024-08-04 11:31:53.017 INFO 10236 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2024-08-04 11:31:53.035 INFO 10236 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2024-08-04 11:31:53.035 INFO 10236 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.68]
2024-08-04 11:31:53.193 INFO 10236 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2024-08-04 11:31:53.193 INFO 10236 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1916 ms
2024-08-04 11:31:53.609 INFO 10236 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2024-08-04 11:31:53.618 INFO 10236 --- [ main] SpingBoot.Hello.HelloApplication : Started HelloApplication in 2.955 seconds (JVM running for 3.484)
打开浏览器访问
打开浏览器输入:http://localhost:8080/hello
为什么要带 hello呢?
是因为,
我们在HelloWorld.java
文件设置了路由规则@RequestMapping("/hello")
,
他里面的/hello
就是http://localhost:8080
后缀/hello
的原因。