资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

如何在SpringBoot中对Swagger进行配置-创新互联

如何在SpringBoot中对Swagger进行配置?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联公司专注于华亭企业网站建设,响应式网站建设,商城网站制作。华亭网站建设公司,为华亭等地区提供建站服务。全流程按需求定制开发,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务

导入SpringBoot集成Swagger所需要的依赖

 
    
      org.springframework.boot
      spring-boot-starter-web
    
    
    
      io.springfox
      springfox-swagger2
      2.9.2
    
    
    
      io.springfox
      springfox-swagger-ui
      2.9.2
    

Swagger可视化界面可分为三个区域

如何在SpringBoot中对Swagger进行配置

Swagger相关配置

package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;

import java.util.ArrayList;

@Configuration
@EnableSwagger2 //开启Swagger的使用
public class SwaggerConfig {

  @Bean  //Swagger的使用主要是要将docket对象传入IOC容器
  public Docket docket(){
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo()) //关于文档的各种信息
        .enable(true) //使Swagger生效
        .groupName("常安祖")
        .select()//选择扫描的接口
        .apis(RequestHandlerSelectors.basePackage("com.example.controller"))//指定扫描的接口
        .build();
  }


  public ApiInfo apiInfo(){
    Contact contact = new Contact("长安","https://blog.csdn.net/weixin_45647685","719801748@qq.com");//个人的联系方式
    return new ApiInfo("长安的文档", "长安的开发文档", "1.0", "urn:tos",null, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());//文档的各种信息
  }
}

@ApiModel( ) //主要用来标注返回的实体类
@ApiModelProperty( ) //主要用来标注实体类中的属性
案例:

@ApiModel("用户的实体类")
public class User implements Serializable {

  @ApiModelProperty("用户的id")
  private Integer id;

  @ApiModelProperty("用户的姓名")
  private String name;

  @ApiModelProperty("用户的年纪")
  private Integer age;

  public Integer getId() {
    return id;
  }
  public User(Integer id, String name, Integer age) {
    this.id = id;
    this.name = name;
    this.age = age;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Integer getAge() {
    return age;
  }

  public void setAge(Integer age) {
    this.age = age;
  }
}

@ApiModelProperty用来标注API接口
案例:

package com.yangzihao.controller;

import com.yangzihao.entity.User;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class UserController {

  @ApiModelProperty("得到一个User")
  @GetMapping("/getUser")
  public User getUser(){
    return new User(1,"测试",18);
  }
}

关于如何在SpringBoot中对Swagger进行配置问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


新闻名称:如何在SpringBoot中对Swagger进行配置-创新互联
本文来源:http://www.cdkjz.cn/article/dhsdgg.html
十二年 建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220