一、首先准备一些工具,使用的是idea和,maven管理工具,springboot使用2.0.2版本
父pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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>springboot-demo</groupId> <artifactId>springboot-demo</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <modules> <module>springboot-1-simple-web</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.44</version> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>建一个springboot-1-simple-web模块
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>springboot-demo</artifactId> <groupId>springboot-demo</groupId> <version>1.0.0</version> </parent> <modelVersion>4.0.0</modelVersion> <packaging>jar</packaging> <artifactId>springboot-1-simple-web</artifactId> </project>子模块已经集成父模块了,所以不需要写springboot的依赖包.
在springboot-1-simple-web模块中建一个启动类:
package com.cn.main; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Created by chenyuncong on 2018/5/21. */ @SpringBootApplication public class SimpleApplication { public static void main(String[] args) { SpringApplication.run(SimpleApplication.class, args); } }在建一个Controller:
package com.cn.main.web; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; /** * Created by chenyuncong on 2018/5/21. */ @RestController @RequestMapping("test") public class TestController { @RequestMapping("getInfo") public Map<String,Object> getInfo(){ Map<String,Object> map=new HashMap<>(); map.put("name","小明同学"); map.put("sex","男"); return map; } }然后启动SimpleApplication类,在浏览器输入 http://127.0.0.1:9988/test/getInfo,返回结果
{
sex: "男",
name: "小明同学"
}这就说明已经好了,返回一个json数据的接口,笔记到此结束了