创建一个Spring Boot HelloWorld项目,
功能:
浏览器发送hello请求,服务器接受请求并处理,响应“Hello World!~”字符串。
1.创建一个maven工程。
2.导入Spring Boot的相关依赖。(在pom.xml文件中添加)
org.springframework.boot spring-boot-starter-parent1.5.9.RELEASE org.springframework.boot spring-boot-starter-web
3.编写一个主程序:启动Spring Boot应用。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
//Spring应用启动起来。
SpringApplication.run(HelloWorldApplication.class,args);
}
}
4.编写相关的Controller、service等。
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")//发送hello请求。
public String hello(){
return "Hello World!~";//返回响应内容“Hello World!~”。
}
}
5.运行主程序测试。
执行完毕后访问网址:
http://localhost:8080/hello
6.简化部署。
导入插件,可将应用打包成一个可执行的jar包。(在pom.xml文件中添加)
org.springframework.boot spring-boot-maven-plugin
在右侧项目栏中点击maven项目,展开项目,运行在生命周期(Lifecycle)目录下的package(双击即可)。
等待执行完毕,执行栏中会给出该jar包的存储位置。
在jar包存储目录下用cmd命令行直接执行:
java -jar jar包名
执行完毕后访问网址:
http://localhost:8080/hello



