程序员我爱编程

SpringBoot笔记(十)swagger2

2018-04-25  本文已影响21人  世外大帝

swagger配置

还是两种配置方式

加入依赖

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
        </dependency>

编写配置类

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",
                        "1876665310@qq.com"))
                .version("1.0")
                .build();
    }


}

效果

效果.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 "<h1>Hello Dear! Let's me Start SpringBoot!</h1>";
    }



}


效果

效果2.png
上一篇下一篇

猜你喜欢

热点阅读