SpringBoot的helloword
1.创建一个Maven工程
在pom.xml中添加一下依赖
<!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> </parent> <!-- Add typical dependencies for a web application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
2.编写一个主程序,启动spring boot应用
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * SpringBootApplication标注一个主程序类,说明这是一个spring boot应用程序 */ @SpringBootApplication public class HelloWordMainAppliaction { public static void main(String [] args){ // SpringApplication.run这个方法使spring启动起来 SpringApplication.run(HelloWordMainAppliaction.class,args); } }
3.编写相关的controller service
package com.example.Controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloWordController { @RequestMapping("/hello") @ResponseBody public String hello(){ return "HelloWord 你好"; } }
4.运行主程序,会自动配置tomcat。
5.访问页面(谷歌浏览器)
localhost:8080/hello
nnjk