SpringBoot写第一个接口
服务可以理解为一个接口,一个controller,一个做业务请求的
新建一个HelloWorldController
1 import org.springframework.boot.SpringApplication; 2 3 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 4 5 import org.springframework.web.bind.annotation.RequestMapping; 6 7 import org.springframework.web.bind.annotation.RestController; 8 9 10 11 import java.util.HashMap; 12 13 import java.util.Map; 14 15 16 @EnableAutoConfiguration//做spring的注容器,创建tomcat,开spring的加载,然后类就都可以使用了 17 18 //@RestController 表示该接口全部返回json格式 相当于下面写了一个@ResponseBody 19 20 @RestController 21 22 public class HelloWorldController { 23 24 // @ResponseBody 25 26 @RequestMapping("/index") 27 28 public String index(){ 29 30 return "success"; 31 32 } 33 34 35 36 @RequestMapping("/getMap") 37 38 public Map<String , Object> getMap(){ 39 40 Map<String , Object> result = new HashMap<String , Object>(); 41 42 result.put("errorCode","200"); 43 44 result.put("errorMsg","成功"); 45 46 return result; 47 48 } 49 50 public static void main(String[] args){ //这个函数只运行这一个java文件,因为加入这个@EnableAutoConfiguration注解后默认只扫当前,其他的不访问 51 //主函數運行springboot項目 (主入口) springboot核心是嵌入Tomcat,但是需要运行,而运行需要一个入口,这个就相当于一个入口 52 53 SpringApplication.run(HelloWorldController.class, args); 54 55 } 56 57 }
然后点击左边的三角号运行
当出现这种情况就可以了
然后在浏览器输入http://localhost:8080/index
输入http://localhost:8080/getMap因为是map集合所以是一个json格式
1 Pom.xml文件,在这里我用的war 2 3 <?xml version="1.0" encoding="UTF-8"?> 4 5 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 7 <modelVersion>4.0.0</modelVersion> 8 9 <groupId>com.itmayiedu</groupId> 10 <artifactId>springboot1</artifactId> 11 <version>1.0-SNAPSHOT</version> 12 <packaging>war</packaging> 13 14 <name>springboot1 Maven Webapp</name> 15 <!-- FIXME change it to the project's website --> 16 <url>http://www.example.com</url> 17 18 <properties> 19 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 20 <maven.compiler.source>1.7</maven.compiler.source> 21 <maven.compiler.target>1.7</maven.compiler.target> 22 </properties> 23 24 25 26 <!—引入springboot父类依赖--> 27 <parent> 28 <groupId>org.springframework.boot</groupId> 29 <artifactId>spring-boot-starter-parent</artifactId> 30 <version>1.3.3.RELEASE</version> 31 </parent> 32 <!—springboot-web组件 springmvc+spring+mybatis--> 33 <dependencies> 34 <dependency> 35 <groupId>junit</groupId> 36 <artifactId>junit</artifactId> 37 <version>4.11</version> 38 <scope>test</scope> 39 </dependency> 40 <dependency> 41 <groupId>org.springframework.boot</groupId> 42 <artifactId>spring-boot-starter-web</artifactId> 43 </dependency> 44 </dependencies> 45 46 <!—这个build中的东西一件出来就有了,具体不大清楚,希望哪位大佬看到帮忙解释下,万分感谢!!!--> 47 <build> 48 <finalName>springboot1</finalName> 49 <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> 50 <plugins> 51 <plugin> 52 <artifactId>maven-clean-plugin</artifactId> 53 <version>3.0.0</version> 54 </plugin> 55 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> 56 <plugin> 57 <artifactId>maven-resources-plugin</artifactId> 58 <version>3.0.2</version> 59 </plugin> 60 <plugin> 61 <artifactId>maven-compiler-plugin</artifactId> 62 <version>3.7.0</version> 63 </plugin> 64 <plugin> 65 <artifactId>maven-surefire-plugin</artifactId> 66 <version>2.20.1</version> 67 </plugin> 68 <plugin> 69 <artifactId>maven-war-plugin</artifactId> 70 <version>3.2.0</version> 71 </plugin> 72 <plugin> 73 <artifactId>maven-install-plugin</artifactId> 74 <version>2.5.2</version> 75 </plugin> 76 <plugin> 77 <artifactId>maven-deploy-plugin</artifactId> 78 <version>2.8.2</version> 79 </plugin> 80 </plugins> 81 </pluginManagement> 82 </build> 83 </project>