Eclipse下使用Maven建立简单Springboot程序
1、创建Maven工程
2、编写pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.niiam.ppweb</groupId> 5 <artifactId>ppweb</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>ppweb Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 11 <parent> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-starter-parent</artifactId> 14 <version>1.5.9.RELEASE</version> 15 </parent> 16 17 <dependencies> 18 <dependency> 19 <groupId>org.springframework.boot</groupId> 20 <artifactId>spring-boot-starter-web</artifactId> 21 </dependency> 22 <dependency> 23 <groupId>junit</groupId> 24 <artifactId>junit</artifactId> 25 <version>3.8.1</version> 26 <scope>test</scope> 27 </dependency> 28 29 </dependencies> 30 <build> 31 <finalName>ppweb</finalName> 32 </build> 33 </project>
编写完后,右键项目选择Maven->Update Project(如项目无报错,则不需要)。创建Java的Source Folder,结构如下图:
解决Eclipse建立Maven项目后无法建立src/main/java资源文件夹的办法
在项目上右键选择properties,然后点击java build path,在Librarys下,编辑JRE System Library,选择workspace default jre就可以了。
测试Springboot
新建类:
1 package com.niiam.ppweb; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RestController; 7 8 @RestController 9 @EnableAutoConfiguration 10 public class AppleApplication { 11 @RequestMapping("/") 12 String home() { 13 return "Hello World!"; 14 } 15 public static void main(String[] args) { 16 SpringApplication.run(AppleApplication.class, args); 17 } 18 }
右键选择run as->java application
打开浏览器,http://localhost:8080/即可看到