java集成swagger

项目目录
在这里插入图片描述

pom依赖

        <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>

HomeController


import java.util.ArrayList;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(tags = "TestController") // 注意不要带中文,扩展时按钮无效
public class HomeController {

    @ApiOperation(value="hello", notes="无参数GET请求测试")
    @GetMapping("/hello")
    public String hello() {
        return "Hello World!";
    }

    @ApiOperation(value = "hello2", notes = "单简单参数")
    @ApiImplicitParam(name = "name", value = "姓名", paramType = "query", dataType = "String")
    @GetMapping("/hello2")
    public String hello2(@RequestParam String name) {
        return "Hello " + name;
    }

    @ApiOperation(value = "hello3", notes = "多简单参数")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "name", value = "姓名", paramType = "query", dataType = "string"),
        @ApiImplicitParam(name = "age", value = "年龄", paramType = "query", dataType = "int")
    })
    @GetMapping("/hello3")
    public String hello3(@RequestParam String name, @RequestParam Integer age) {
        return "Hello " + name + ", age " + age.toString();
    }

    @ApiOperation(value = "hello4", notes = "传递对象参数")
    @ApiImplicitParam(name = "person", value = "人", paramType = "body", dataType = "Person")
    @PostMapping("/hello4")
    public String hello4(@RequestBody Person person) {
        return "Hello, " + person.getName() + " " + person.getAge();
    }

    @ApiOperation(value = "hello5", notes = "传递集合参数")
    @ApiImplicitParam(name = "persons", value = "集合列表", paramType = "body", dataType = "ArrayList<String>")
    @PostMapping("/hello5")
    public String hello5(@RequestBody ArrayList<String> persons) {
        StringBuilder sb = new StringBuilder();
        persons.forEach(t -> sb.append(t).append("\n"));
        return sb.toString();
    }
}

Person

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;


@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Person {
    private String name;
    private Integer age;
}

DemoSwaggerApplication

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class DemoSwaggerApplication {

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

}

Swagger2


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

 
@Configuration
@EnableSwagger2
public class Swagger2 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(this.apiInfo())
            .select()
            .apis(RequestHandlerSelectors.basePackage("cn.ljlin233.demoswagger.controller"))
            .paths(PathSelectors.any())
            .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("springboot利用swagger构建api文档")
            .description("项目描述,代码地址 需要去我目录去找,随便放了个文件上传下载 https://download.youkuaiyun.com/download/qq_42981242/13244485")
            .termsOfServiceUrl("https://download.youkuaiyun.com/download/qq_42981242/13244485")
            .version("1.0")
            .build();
    }

}

application.properties没有东西上面配置好直接运行
http://localhost:8080/swagger-ui.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值