1、idea创建一个maven工程 1)File------>new------->Project 2)maven------->选择自己的jdk版本(1.7以上)--------->next
3)GroupId填写自己项目包名称 Artifactld填写项目名称 点击【Next】
4)Project name 填写第3步输入的项目名称 Project location 填写该项目保存路径
5)上一步完成,项目就创建好了,点击右下角的Enable Auto-Import,自动导入所需要的依赖包。
6)在pom.xml文件中导入spring boot相关的依赖
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
org.springframework.boot
spring-boot-starter-web
2、编写一个主程序;启动Spring Boot应用 1)在java下创建com.rf包 在com.rf包下创建主启动类
package com.rf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
// Spring应用启动起来
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
2)编写相关的Controller、Service 在com.rf包下创建HelloController类
package com.rf.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping("/hello")
@ResponseBody
public String hello(){
return "hello world !";
}
}
3)运行主程序测试 进入主程序类,右键点击 【run HelloWorldMainApplication 】
4)在浏览器中访问 localhost:8080/hello
3、简化部署 1)在pom.xml中引入maven插件
org.springframework.boot
spring-boot-maven-plugin
2)点击【Maven Projects】------>【Lifecycle】------>【packeage】,在左侧target下会生成项目的jar包
3)把jar包复制到桌面
4)打开dos窗口,进入桌面目录
5)java -jar 命令启动
6)浏览器中输入 localhost:8080/hello 访问如下图