SpringBoot笔记(十)swagger2

swagger配置

还是两种配置方式

  • 直接在application中配置
    1. @EnableSwagger2注解
    2. 加入@baen等配置
  • 单独建立swagger2配置类(推荐)

加入依赖

        
            io.springfox
            springfox-swagger2
            2.6.1
        
        
            io.springfox
            springfox-swagger-ui
            2.6.1
        

编写配置类

package com.jiataoyuan.demo.springboot.swagger;

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.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author TaoYuan
 * @version V1.0.0
 * @date 2018/4/19 0019
 * @description description
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket buildDocket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.jiataoyuan.demo.springboot.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo buildApiInfo(){
        return new ApiInfoBuilder()
                .title("Start restfulAPI Demo")
                .description("springboot swagger2")
                .termsOfServiceUrl("https://blog.csdn.net/lftaoyuan")
                .contact(new Contact(
                        "TaoYuan",
                        "https://blog.csdn.net/lftaoyuan",
                        "[email protected]"))
                .version("1.0")
                .build();
    }


}

  • apis的basePackage可以自行设置controller包,详细到包
  • paths一般选择any
  • apiInfo可自行设置,主要是API的标题之类的显示信息,也可以直接build
  • 接口:http://{ip}:{port}/swagger-ui.html

效果

SpringBoot笔记(十)swagger2_第1张图片
效果.png

接口配置

package com.jiataoyuan.demo.springboot.controller;

import com.jiataoyuan.demo.springboot.model.User;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author TaoYuan
 * @version V1.0.0
 * @date 2018/4/12 0012
 * @description description
 */

@RestController
@RequestMapping("/start")
public class StartController {


    // return json
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息note")
    @GetMapping("/getUser")
    public User getUser() {
        User user = new User();
        user.setName("zhangsan");
        return user;
    }

    @ApiOperation(value = "修改用户信息", notes = "修改用户信息note")
    @PostMapping("/setUser")
    public User setUser(@RequestBody @ApiParam(name="User",value="用户信息",required=true) User user) {

        return user;
    }

    // 隐式参数的方式
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "user",
                    value = "用户信息",
                    required = true,
                    dataType = "User")
    })
    @ApiOperation(value = "修改用户信息", notes = "修改用户信息note")
    @PostMapping("/setUser1")
    public User setUser1(@RequestBody User user) {
        return user;
    }

    // main
    @GetMapping()
    public String Main() {
        return "

Hello Dear! Let's me Start SpringBoot!

"; } }

效果

SpringBoot笔记(十)swagger2_第2张图片
效果2.png

你可能感兴趣的:(SpringBoot笔记(十)swagger2)