swagger 基本使用入门

swagger 基本使用入门

  • 1. 新建项目
  • 2. 引入依赖
  • 3. swagger 配置文件
  • 4. demo

1. 新建项目

2. 引入依赖

<dependency>
      <groupId>io.springfoxgroupId>
      <artifactId>springfox-swagger2artifactId>
      <version>2.9.2version>
   dependency>
   <dependency>
      <groupId>io.springfoxgroupId>
      <artifactId>springfox-swagger-uiartifactId>
      <version>2.9.2version>
   dependency>
dependencies>

3. swagger 配置文件

package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

   @Bean
   public Docket createRestApi() {
      return new Docket(DocumentationType.SWAGGER_2)
            .pathMapping("/")
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
            .paths(PathSelectors.any())
            .build()
            .apiInfo(new ApiInfoBuilder()
                  .title("springboot 整合 swagger")//接口文档的标题
                  .description("springboot 整合 swagger test")
                  .version("test 01")
                  .contact(new Contact("name", "url", "email"))    // 开发文档的作者信息
                  .license("license") //license 规范
                  .licenseUrl("licenseUrl")
                  .build());
   }
}

访问地址:http://localhost:8080/swagger-ui.html

4. demo

package com.example.demo.controller;

import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("user")
@Api(tags = "用户服务接口")
public class UserController {

	@GetMapping("findAll")
	@ApiOperation(value = "查询所有用户信息", notes = "查询所有用户信息接口")
	public Map<String, Object> findAll() {
		log.info("findAll");
		Map<String, Object> map = new HashMap<>();
		map.put("success", "查询所有成功");
		map.put("state", true);
		return map;
	}

	@PostMapping("save")
	@ApiOperation(value = "保存用户信息", notes = "保存用户信息接口")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "用户id", dataType = "String", defaultValue = "21"),
			@ApiImplicitParam(name = "name", value = "用户名", dataType = "String", defaultValue = "zhangsan")
	})
	@ApiResponses({
			@ApiResponse(code = 404, message = "页面不存在"),
			@ApiResponse(code = 500, message = "服务器出错")
	})
	public Map<String, Object> sve(String id, String name) {
		log.info("findAll");
		Map<String, Object> map = new HashMap<>();
		map.put("success", "保存用户信息");
		map.put("state", true);
		return map;
	}
}

你可能感兴趣的:(springboot,spring,boot,swagger)