springboot学习笔记(2)创建第一个springboot项目

创建一个maven项目(jar类型),在pom文件中引入springboot的父类依赖。(jdk1.8)


 
 
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version> </parent> <dependencies>
        <!-- springboot - web组件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>

使用springboot写第一个接口(服务)
写一个controller  : HelloWorldController
 
 
import java.util.HashMap; import java.util.Map; 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;
//表示开始注入spring容器,Tomcat的加载 @EnableAutoConfiguration
//表示该接口全部返回json格式 @RestController public class HelloWorldController {         //@ResponseBody @RequestMapping("/index") public String index(){ return "success"; } @RequestMapping("/getMap") public Map<String,Object> getMap(){ Map<String,Object> result = new HashMap<String,Object>(); result.put("errorCode", "200"); result.put("errorMsg", "成功.."); return result; } public static void main(String[] args) {
                 //主函数开始运行springboot项目 SpringApplication.run(HelloWorldController.class, args); } }

成功运行后,浏览器上访问 localhost:8080/index
localhost:8080/getMap(返回一个json格式数据)

猜你喜欢

转载自blog.csdn.net/huermiss/article/details/80258018