环境要求
- java 8
- IDEA编译器
- maven 版本>=3.6
功能表述
浏览器发送hello请求,服务器接收并且处理,响应hello world字符串。
步骤
1. 创建maven工程
2. 导入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3. 创建主程序
package com.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @SpringBootApplication标注主程序
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
/**
* spring应用启动
*/
SpringApplication.run(HelloWorldMainApplication.class, args);
}
}
4. 编写业务逻辑
在controll层实现hello接口返回hello world.
package com.springboot.controller;
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")
public String hello(){
return "hello world!";
}
}
5. 运行主程序
运行主程序,在浏览器中打开接口。就可以轻松的访问数据
6. 使用spring plugin打包
使用本插件可以将应用打包成可执行的jar
<!-- 使用本插件可以将应用打包成一个可执行的jar-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.9.RELEASE</version>
</plugin>
</plugins>
</build>
双击package可以将项目打包成jar
打包成功如下图,可以在cmd命令中直接运行。 Java - jar运行