一、Spring Boot HelloWorld
一个功能:
浏览器发送hello请求,服务器接受请求并处理,响应相应结果
1、创建一个maven工程;(jar)
2、导入Spring Boot依赖
<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.liudong;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication//标注主程序类 ,表明这是一个SpringBoot应用
public class HelloWorldMainApplication
{
public static void main(String[] args)
{
//启动Spring应用
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
4、编写相关Controller、Service
package com.liudong.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、简化部署
1、添加插件
<!--添加插件——将应用打包成可执行jar包-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>