微服务实战(二) 搭建第一个SpringBoot小应用

1、搭建第一个springboot微服务

创建一个maven工程,工程结构如下:

pom.xml内容如下:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.zjf.csdn</groupId>
	<artifactId>combat1</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<description>zjf微服务实战1</description>
	<name>combat1</name>
	<url>https://blog.csdn.net/u011177064</url>


	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- Lombok -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-dependencies</artifactId>
				<version>2.1.1.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</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

接口调试:(使用postman进行调用)

接口返回:

发布了24 篇原创文章 · 获赞 75 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/u011177064/article/details/104101766