快速搭建一个restful风格的springboot项目
1.创建一个工程。
2.引入pom.xml依赖,如下
<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.xiaowu</groupId> <artifactId>springBootDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3.这个时候项目会提示报错,如下:
4.根据提示,解决问题。
5.操作如下:
6.编写domain类
package com.xiaowu.domain; public class Phone { private String phoneName; private String phoneNumber; public String getPhoneName() { return phoneName; } public void setPhoneName(String phoneName) { this.phoneName = phoneName; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } }
7.编写controller类
package com.xiaowu.controller; import java.util.HashMap; import java.util.Map; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value="/phone") public class PhoneController { @RequestMapping public String index(){ return "Hello World!"; } @RequestMapping(value="/getmap") public Map<String,String> getMap(){ Map<String,String> phoneMap = new HashMap<String,String>(); phoneMap.put("name", "apple phone"); phoneMap.put("money", "5000"); return phoneMap; } }
8.编写启动类
package com.xiaowu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
9.这个时候,直接启动的话,不好意思会报错,如下。
因为我们引入了
</dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
,但是刚才的启动中没有在项目的配置文件中引入mysql数据库的相关信息,所以报错。
10.引入配置文件即可,当然我们的数据库中要真实的存在相关的表。
11.再次启动,OK.
12.使用Chrome接口测试工具测试效果。
至此,一个简单的基于SpringBoot的Restful WebService搭建完成。