先来看一下最终配置完的示意图,浏览器输入localhost:8080/hello,出现Hello World!
之前网上看了很多教程都不是很详细,对我这样刚接触java的不是很友好,所以决定自己写一篇有详细步骤的文章。
第一步:新建一个Maven工程
我用的是IDEA,没有的自己下一个,网上教程很多
启动IDEA,点击Create New Project
选择Maven项目,选择JDK环境,我的是11,然后点击next
GroupId随便填,ArtifactId填的是项目名:HelloWorld,然后点击next
默认如图,项目名和项目存放位置,点击finish
第一次进去会有选择手动/自动导入Maven项目,我们选择自动导入,这样以后每写一个依赖IDEA会帮我们自动导入
点击Enable Auto-Import
第二步:导入Spring Boot的相关依赖
刚创建完是这样的页面,左侧为项目列表,右侧为代码编辑区
对pom.xml进行配置
添加如下代码,IDEA会自动导入包,在左侧的External Libraries能看到导入的包
<parent>
<groupId>org.springframework.boot</groupId>>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
配置完是这样的
第三步:编写一个主程序,用来启动Spring Boot应用
src —> main —> java —> 右击new —> java class
com.cgh是包名,后面的HelloWorldApplication是类名
新建完之后如图
编写主程序代码:
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class,args);
}
}
主程序如图,注意程序中有注解
第四步:编写相关的Controller、Service
src —> main —> java —> com.cgh —> 右击new —> java class
controller是包名,HelloController是类名
新建完之后如图,注意目录结构
编写代码
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World!";
}
}
Controller如图,注意有三个注解
第五步:运行主程序测试
到主程序的main方法中启动测试,或者点击最上面一排工具栏中的Run按钮也可以
运行这样的表示启动成功
"/Applications/IntelliJ IDEA.app/Contents/jbr/Contents/Home/bin/java" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=57263:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Users/caiguanhong/HelloWorld/target/classes:/Users/caiguanhong/.m2/repository/org/springframework/boot/spring-boot-starter-web/2.2.0.RELEASE/spring-boot-starter-web-2.2.0.RELEASE.jar:/Users/caiguanhong/.m2/repository/org/springframework/boot/spring-boot-starter/2.2.0.RELEASE/spring-boot-starter-2.2.0.RELEASE.jar:/Users/caiguanhong/.m2/repository/org/springframework/boot/spring-boot/2.2.0.RELEASE/spring-boot-2.2.0.RELEASE.jar:/Users/caiguanhong/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.2.0.RELEASE/spring-boot-autoconfigure-2.2.0.RELEASE.jar:/Users/caiguanhong/.m2/repository/org/springframework/boot/spring-boot-starter-logging/2.2.0.RELEASE/spring-boot-starter-logging-2.2.0.RELEASE.jar:/Users/caiguanhong/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/Users/caiguanhong/.m2/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/Users/caiguanhong/.m2/repository/org/slf4j/slf4j-api/1.7.28/slf4j-api-1.7.28.jar:/Users/caiguanhong/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.12.1/log4j-to-slf4j-2.12.1.jar:/Users/caiguanhong/.m2/repository/org/apache/logging/log4j/log4j-api/2.12.1/log4j-api-2.12.1.jar:/Users/caiguanhong/.m2/repository/org/slf4j/jul-to-slf4j/1.7.28/jul-to-slf4j-1.7.28.jar:/Users/caiguanhong/.m2/repository/jakarta/annotation/jakarta.annotation-api/1.3.5/jakarta.annotation-api-1.3.5.jar:/Users/caiguanhong/.m2/repository/org/springframework/spring-core/5.2.0.RELEASE/spring-core-5.2.0.RELEASE.jar:/Users/caiguanhong/.m2/repository/org/springframework/spring-jcl/5.2.0.RELEASE/spring-jcl-5.2.0.RELEASE.jar:/Users/caiguanhong/.m2/repository/org/yaml/snakeyaml/1.25/snakeyaml-1.25.jar:/Users/caiguanhong/.m2/repository/org/springframework/boot/spring-boot-starter-json/2.2.0.RELEASE/spring-boot-starter-json-2.2.0.RELEASE.jar:/Users/caiguanhong/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.10.0/jackson-databind-2.10.0.jar:/Users/caiguanhong/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.10.0/jackson-annotations-2.10.0.jar:/Users/caiguanhong/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.10.0/jackson-core-2.10.0.jar:/Users/caiguanhong/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jdk8/2.10.0/jackson-datatype-jdk8-2.10.0.jar:/Users/caiguanhong/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.10.0/jackson-datatype-jsr310-2.10.0.jar:/Users/caiguanhong/.m2/repository/com/fasterxml/jackson/module/jackson-module-parameter-names/2.10.0/jackson-module-parameter-names-2.10.0.jar:/Users/caiguanhong/.m2/repository/org/springframework/boot/spring-boot-starter-tomcat/2.2.0.RELEASE/spring-boot-starter-tomcat-2.2.0.RELEASE.jar:/Users/caiguanhong/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.27/tomcat-embed-core-9.0.27.jar:/Users/caiguanhong/.m2/repository/org/apache/tomcat/embed/tomcat-embed-el/9.0.27/tomcat-embed-el-9.0.27.jar:/Users/caiguanhong/.m2/repository/org/apache/tomcat/embed/tomcat-embed-websocket/9.0.27/tomcat-embed-websocket-9.0.27.jar:/Users/caiguanhong/.m2/repository/org/springframework/boot/spring-boot-starter-validation/2.2.0.RELEASE/spring-boot-starter-validation-2.2.0.RELEASE.jar:/Users/caiguanhong/.m2/repository/jakarta/validation/jakarta.validation-api/2.0.1/jakarta.validation-api-2.0.1.jar:/Users/caiguanhong/.m2/repository/org/hibernate/validator/hibernate-validator/6.0.17.Final/hibernate-validator-6.0.17.Final.jar:/Users/caiguanhong/.m2/repository/org/jboss/logging/jboss-logging/3.4.1.Final/jboss-logging-3.4.1.Final.jar:/Users/caiguanhong/.m2/repository/com/fasterxml/classmate/1.5.0/classmate-1.5.0.jar:/Users/caiguanhong/.m2/repository/org/springframework/spring-web/5.2.0.RELEASE/spring-web-5.2.0.RELEASE.jar:/Users/caiguanhong/.m2/repository/org/springframework/spring-beans/5.2.0.RELEASE/spring-beans-5.2.0.RELEASE.jar:/Users/caiguanhong/.m2/repository/org/springframework/spring-webmvc/5.2.0.RELEASE/spring-webmvc-5.2.0.RELEASE.jar:/Users/caiguanhong/.m2/repository/org/springframework/spring-aop/5.2.0.RELEASE/spring-aop-5.2.0.RELEASE.jar:/Users/caiguanhong/.m2/repository/org/springframework/spring-context/5.2.0.RELEASE/spring-context-5.2.0.RELEASE.jar:/Users/caiguanhong/.m2/repository/org/springframework/spring-expression/5.2.0.RELEASE/spring-expression-5.2.0.RELEASE.jar com.cgh.HelloWorldApplication
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.0.RELEASE)
2020-01-05 16:34:25.368 INFO 12922 --- [ main] com.cgh.HelloWorldApplication : Starting HelloWorldApplication on caiguanhongdeMacBook-Air.local with PID 12922 (/Users/caiguanhong/HelloWorld/target/classes started by caiguanhong in /Users/caiguanhong/HelloWorld)
2020-01-05 16:34:25.372 INFO 12922 --- [ main] com.cgh.HelloWorldApplication : No active profile set, falling back to default profiles: default
2020-01-05 16:34:27.904 INFO 12922 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-01-05 16:34:27.942 INFO 12922 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-01-05 16:34:27.942 INFO 12922 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.27]
2020-01-05 16:34:28.166 INFO 12922 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-01-05 16:34:28.167 INFO 12922 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2623 ms
2020-01-05 16:34:28.557 INFO 12922 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-01-05 16:34:29.050 INFO 12922 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-01-05 16:34:29.059 INFO 12922 --- [ main] com.cgh.HelloWorldApplication : Started HelloWorldApplication in 4.686 seconds (JVM running for 6.249)
然后我们就可以进行测试了,在浏览器中输入localhost:8080
出现这样的没关系,接着输入/hello
这样就成功了,是不是很简单呢!
如果看了这篇文章对你有帮助的话点个赞再走喔~