1、搭建第一个springboot微服务

创建一个maven工程,工程结构如下:
二、搭建第一个SpringBoot小应用 - 图1
pom.xml内容如下:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.zjf.csdn</groupId>
  6. <artifactId>combat1</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <description>zjf微服务实战1</description>
  9. <name>combat1</name>
  10. <url>https://blog.csdn.net/u011177064</url>
  11. <dependencies>
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-web</artifactId>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-test</artifactId>
  19. <scope>test</scope>
  20. </dependency>
  21. <!-- Lombok -->
  22. <dependency>
  23. <groupId>org.projectlombok</groupId>
  24. <artifactId>lombok</artifactId>
  25. </dependency>
  26. </dependencies>
  27. <dependencyManagement>
  28. <dependencies>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-dependencies</artifactId>
  32. <version>2.1.1.RELEASE</version>
  33. <type>pom</type>
  34. <scope>import</scope>
  35. </dependency>
  36. </dependencies>
  37. </dependencyManagement>
  38. <build>
  39. <plugins>
  40. <plugin>
  41. <groupId>org.apache.maven.plugins</groupId>
  42. <artifactId>maven-compiler-plugin</artifactId>
  43. <version>3.1</version>
  44. <configuration>
  45. <source>1.8</source>
  46. <target>1.8</target>
  47. </configuration>
  48. </plugin>
  49. </plugins>
  50. </build>
  51. </project>

application.yml内容如下

server:
  port: 9999

SimpleApi.java 内容如下 (实现一个简单的POST接口)

package com.zjf.combat.api.simple;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjf.combat.common.RespData;

 /**
  * 微服务实战测试API
  * @author zhaojunfu
  *
  */
 @RestController
 @RequestMapping(value = "/api/simple")
 public class SimpleApi {


     @RequestMapping(value = "/test", method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE)
 public RespData test( @RequestBody SimpleForm form ) {

         return new RespData("hello "+form.getName());
     }
 }

SimpleForm.java的内容如下:(主要作为SimpleApi接口中的表单实体)

package com.zjf.combat.api.simple;

import java.io.Serializable;

import lombok.Data;

@Data
public class SimpleForm  implements Serializable  {

     private String name;

 }

RespData.java的内容如下:

package com.zjf.combat.common;


public class RespData{
    public static final int Success = 1;
    public static final int Error = 0;

    private int status;
    private Object datas = "";
     private Object meta = "";

     public RespData(Object datas) {
         this.datas = datas;
         this.status =Success;
     }


     public RespData(int status, Object datas) {
         this.status = status;
         if(status == Success){
             this.datas = datas;
         }else{
             this.meta = datas;
         }
     }



     public RespData(Exception e){
         this.status = Error;
         this.meta = e.getMessage();
     }

     public RespData() {
     }

     public int getStatus() {
         return status;
     }
     public void setStatus(int status) {
         this.status = status;
     }
     public Object getDatas() {
         return datas;
     }
     public void setDatas(Object datas) {
         this.datas = datas;
     }
     public Object getMeta() {
         return meta;
     }
     public void setMeta(Object meta) {
         this.meta = meta;
     }



 }


MainApplication.java 内容如下:(作为Springboot的启动类,默认它是扫描同级包以及同级的子包下的java类)

package com.zjf.combat;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * boot 启动器  
  * @author zhaojunfu
  *
  */
 @Configuration
 @EnableAutoConfiguration
 @ComponentScan
 public class MainApplication {

     public static void main(String[] args) {
          SpringApplication.run(MainApplication.class, args);
     }

 }


到此第一个SpringBoot应用就搭建好了,直接运行MainApplication
二、搭建第一个SpringBoot小应用 - 图2

接口调试:(使用postman进行调用)
二、搭建第一个SpringBoot小应用 - 图3

接口返回:
二、搭建第一个SpringBoot小应用 - 图4