前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Springboot+Swagger2

Springboot+Swagger2

作者头像
用户1212940
发布2022-04-13 13:44:24
1790
发布2022-04-13 13:44:24
举报
文章被收录于专栏:LambdaLambda

springboot+swagger2

http://start.spring.io/生成springboot工程

11464886-2aeeecc715d6f9c2.jpg
11464886-2aeeecc715d6f9c2.jpg

引入maven依赖

代码语言:javascript
复制
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.7.0</version> 
</dependency> 
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.7.0</version> 
</dependency>

加载swagger2配置

代码语言:javascript
复制
@Configuration 
@EnableSwagger2 
public class SwaggerConfig {
  @Autowired
  Environment env;
  @Bean
  public Docket createRestApi(ApiInfo apiInfo) {
        return new Docket(DocumentationType.*SWAGGER_2*)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage(env.getProperty("swagger.scan")))
                .paths(PathSelectors.any())
                .build();
  }

    @Bean
  public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(env.getProperty("swagger.title"))
                .description(env.getProperty("swagger.description"))
                .version(env.getProperty("swagger.version"))
                .build();
  }
}

application.yml

代码语言:javascript
复制
spring:
  application:
    name: swagger-test
server:
  port: 8080
  context-path: /api
swagger:
  scan: com.swagger.test
  title: 用户service API
  description: 用户service API
  version: 1.0.0

controller

代码语言:javascript
复制
@RestController
@RequestMapping("/user")
@Api(value="用户controller",description="用户操作",tags={"用户操作接口"})
public class UserController {
    @GetMapping("/get")
    @ApiOperation(value = "获取用户接口",notes = "获取用户接口notes",response = String.class)
    public String get(@ApiParam(name = "userName",value = "用户名",required = true) @RequestParam("userName") String userName){

        return "返回值:"+userName;
    }
    @PostMapping("/post")
    @ApiOperation(value = "获取用户接口Post",notes = "获取用户接口notes Post",response = UserForm.class)
    public UserForm post(@RequestBody @ApiParam(name="用户对象",value="传入json格式",required=true)  UserForm userForm){
        return userForm;
    }
}

form

代码语言:javascript
复制
@ApiModel(description = "用户from")
public class UserForm {
    @ApiModelProperty(value = "用户名",name = "userName",required = true)
    private String userName;

    @Override
    public String toString() {
        return "UserForm{" +
                "userName='" + userName + '\'' +
                '}';
    }

    /**
     * Getter method for property <tt>userName</tt>
     *
     * @return property value of userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * Setter method for property <tt>userName</tt>.
     *
     * @param userName value to be assigned to property userName
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }
}

启动类:

代码语言:javascript
复制
@EnableSwagger2
@SpringBootApplication
@RestController
public class DemoApplication {

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

启动 访问:http://localhost:8080/api/swagger-ui.html

例子下载地址:http://download.csdn.net/download/zyj_2012/10191998

11464886-78e002ba6cf43da6.jpg
11464886-78e002ba6cf43da6.jpg
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-07-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • springboot+swagger2
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档