IDE中创建maven项目
最简pom依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
示例代码 hello/SampleController.java
package hello;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/sayHello")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
测试
- 运行main方法
- 浏览器访问: localhost:8080/sayHello
- 页面会显示Hello World!
- 学习Spring Boot的Hello World程序已成功carry
打成可执行JAR
如上的代码在IDE中执行,毫无问题
但是怎么把代码部署到服务器上去呢?
需要在pom中增加如下的插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
mvn clean package打成可执行jar后,放到服务器上
java -jar springbootDemo.jar
观察启动日志,如果没报错
在浏览器访问http://${ip}:${port}/tt如果返回Hello World!说明大功告成