1.先对pom.xml进行编写
引入springboot的父类依赖(这样spring的web组件将可以少写很多,maven有继承依赖机制)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
添加依赖
<!-- springboot-web组件 springmvc+spring+mybatis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入freeMarker的依赖包. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</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>
注意:如果报错Cannot determine embedded database driver class for database type NONE.请删除
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</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>
2.新添加一个类(里面有注释,启动类和控制器写一起了)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/*
*自动装载外部项目@Configuration配置类
*/
@EnableAutoConfiguration
/*
*接口返回JSON的注解
*/
@RestController
public class TestController {
@RequestMapping("/index")
public String index(){
return "success";
}
@RequestMapping("/getMap")
public Map<String,Object> getMap(){
Map<String,Object> map = new HashMap<>();
map.put("errorCode","666");
map.put("errorMessage","错误消息");
return map;
}
public static void main(String[] args) {
//主函数运行springboot
SpringApplication.run(TestController.class,args);
}
}
3.先maven的clear,install然后在运行程序
在浏览器输入http://localhost:8080/getMap,得到: