Spring Boot要求Maven的版本达到3.2或以上。
实例:
POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.springboottest</groupId> <artifactId>springboottest1</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboottest1</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.7.RELEASE</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <!-- Add typical dependencies for a web application --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
TestController.java:
package com.jsoft.springboottest.springboottest1.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @RequestMapping("/show") public String show(){ return "Hello World"; } }
App.java:
package com.jsoft.springboottest.springboottest1; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Hello world! * */ @SpringBootApplication public class App { public static void main( String[] args ) { SpringApplication.run(App.class, args); } }
运行:
#第一种 mvn spring-boot:run #第二种 mvn package打包之后,使用java -jar xxx.jar运行 #第三种 直接Eclipse的Run As指定到App的入口main方法即可。
访问:
测试工程:
https://github.com/easonjim/5_java_example/tree/master/springboottest/springboottest1
参考: