swagger 接口测试

废话不多说,直接上代码

 

1 pom 添加

 

复制代码
<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
        </dependency>
复制代码

 

2 增加SwaggerConfig

复制代码
package com.test.service1;
/**
 * Date: 2020/6/24 11:36
 *
 * @author Tyler
 */
 
import io.swagger.annotations.ApiOperation;
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;
 
/**
 *@author Tyler
 *@date 2020/6/24
 */
 
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }
 
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API Document")
                .description("API Document")
                .version("1.0")
                .build();
    }
}
复制代码

 

3 controller 添加注解

注意:paramType="query" 这个不加,获取不到参数

复制代码
@RestController
@RequestMapping("test")
public class TestController {
    @Autowired
    testService testService;

    @ApiOperation(value = "/hello", notes = "测试hello")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "str",value="str",dataType = "string")
            ,@ApiImplicitParam(paramType="query",name = "age",value="hello age",dataType = "int")})
    @GetMapping(value = "/hello")
    public String hello(String str,Integer age){
        String s =str+":" +age;
        return s;
    }

    @PostMapping(value="/test")
    @ApiOperation(value = "/test", notes = "test")
    public Page<Test> test(@ModelAttribute User u)
    {
        Page<Test> page=testService.findList("t3");
        return page;
    }

}
复制代码

使用@ModelAttribute修饰复杂属性。

 

4 运行  http://localhost:9040/swagger-ui.html#

注意:修改成自己的端口

 

 

参考:

https://www.cnblogs.com/jin-zhe/p/8241368.html

 

posted @   正怒月神  阅读(7582)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-07-01 Aop 简单实例
2019-07-01 幂等 zuul的Filter实现
点击右上角即可分享
微信分享提示