SpringBoot+Swagger2配置

本文档介绍了如何在SpringBoot项目中配置Swagger2,包括引入依赖、配置 Swagger2、创建接口、启用文档查看,以及如何改版Swagger的展示样式,提供更舒适的使用体验。
摘要由CSDN通过智能技术生成

1. 引入jar包依赖

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

2. 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(apiInfo())
                .select()
                //扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.siwei.insurance.productManage"))
                .paths(PathSelectors.any())
                .build();
    }
    
    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("寿险")
                //描述
                .description("接口文档")
                //创建人
                //.contact(new Contact("LinXiuNan", "", ""))
                //版本号
                .version("1.0")
                .build();
    }
 
 
}

将该配置文件放在和Application启动类同等目录下即可

3. 创建接口

import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.insurance.entity.InsuranceCompany;
import com.siwei.insurance.helper.CodeConstant;
import com.siwei.insurance.helper.PageConstant;
import com.siwei.insurance.helper.PageResponseBody;
import com.siwei.insurance.helper.ResponseBody;
import com.siwei.insurance.productManage.service.InsuranceCompanyService;
import com.siwei.insurance.until.BindingResultUtil;

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

/**
 * 保险公司Controller
 * @author linxiunan
 * @date 2018年7月25日
 */
@RestController
@RequestMapping("/insureCompany")
@Api(tags = "保险公司Controller")
public class InsuranceCompanyController {
	
	@Autowired
	private InsuranceCompanyService insuranceCompanyService;
	
	private static final String UNIQUE_EMPTY_ERROR = "保险公司编码不能为空";

	@ApiOperation(value = "添加一条保险公司信息", notes = "添加一条保险公司信息")
	@PostMapping("/insertInsureCompany")
	public ResponseBody insertInsureCompany(@Valid Insu
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值