SpringBoot写第一个接口

服务可以理解为一个接口,一个controller,一个做业务请求的

      

       新建一个HelloWorldController

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;

@EnableAutoConfiguration//做spring的注容器,创建tomcat,开spring的加载,然后类就都可以使用了
//@RestController 表示该接口全部返回json格式  相当于下面写了一个@ResponseBody
@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項目 (主入口)    springboot核心是嵌入Tomcat,但是需要运行,而运行需要一个入口,这个就相当于一个入口
       
SpringApplication.run(HelloWorldController.class, args);
    }
}
 
 

然后点击左边的三角号运行

 

当出现这种情况就可以了

  

然后在浏览器输入http://localhost:8080/index

 

输入http://localhost:8080/getMap因为是map集合所以是一个json格式

 

Pom.xml文件,在这里我用的war

<?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>com.itmayiedu</groupId>
  <artifactId>springboot1</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>springboot1 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
 
<url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

       

<!—引入springboot父类依赖-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>
<!—springboot-web组件 springmvc+spring+mybatis-->
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
  </dependencies>

<!—这个build中的东西一件出来就有了,具体不大清楚,希望哪位大佬看到帮忙解释下,万分感谢!!!-->
  <build>
    <finalName>springboot1</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
     
<plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
       
<plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

猜你喜欢

转载自www.cnblogs.com/xiangzhaogenvpengyou/p/8979193.html