Spring Boot

Spring官方网站的位置

https://spring.io/

 

Spring Boot 初始项目的位置

http://start.spring.io/

 

创建Spring Boot 工程

 

创建好的工程

 

运行程序

 1 package com.mannycat.springbootlesson1;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 
 6 @SpringBootApplication
 7 public class SpringBootLesson1Application {
 8 
 9     public static void main(String[] args) {
10         SpringApplication.run(SpringBootLesson1Application.class, args);
11     }
12 }

 

控制台会输出很多重要的信息

 

访问 http://localhost:8080/

 

修改代码

 1 package com.mannycat.springbootlesson1;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.ResponseBody;
 8 
 9 @Controller
10 @SpringBootApplication
11 public class SpringBootLesson1Application {
12 
13     public static void main(String[] args) {
14         SpringApplication.run(SpringBootLesson1Application.class, args);
15     }
16     
17     @RequestMapping("/")
18     @ResponseBody
19     String home() {
20         return "Hello World!";
21     }
22 }

 

重新启动,看控制台的输出"/"对应了hom()方法

 

重新访问http://localhost:8080

 

继续添加rest风格请求

 1 package com.mannycat.springbootlesson1;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.ResponseBody;
 8 
 9 import java.util.HashMap;
10 import java.util.Map;
11 
12 @Controller
13 @SpringBootApplication
14 public class SpringBootLesson1Application {
15 
16     public static void main(String[] args) {
17         SpringApplication.run(SpringBootLesson1Application.class, args);
18     }
19 
20     @RequestMapping("/")
21     @ResponseBody
22     String home() {
23         return "Hello World!";
24     }
25 
26     @RequestMapping("/rest")
27     @ResponseBody
28     public Map<String, Object> rest() {
29         Map<String, Object> data = new HashMap<String, Object>();
30 
31         data.put("1", "A");
32         data.put("2", 2);
33         
34         return data;
35     }
36 }

 

 从控制台查看是否配置成功

 

访问http://localhost:8080/rest

 

修改application.properties文件,修改端口

 

查看控制台是否修改成功

 

 访问http://localhost:7001/

 

在POM里添加依赖

 

application.properties配置文件加上

 

控制台同时启动8080和8081

 

 8081会启动管理运维

http://localhost:8081/actuator

 

posted @ 2018-07-19 16:47  manny_cat  阅读(126)  评论(0编辑  收藏  举报