SpringBoot文档中心配置

SpringBoot文档中心配置,第1张

SpringBoot文档中心配置 1.导入依赖
        
            org.springframework.plugin
            spring-plugin-core
            2.0.0.RELEASE
        
        
            org.springframework.plugin
            spring-plugin-metadata
            2.0.0.RELEASE
        
        
            io.springfox
            springfox-boot-starter
            3.0.0
            
                
                    org.springframework.plugin
                    spring-plugin-core
                
                
                    org.springframework.plugin
                    spring-plugin-metadata
                
            
        

        
        
            com.github.xiaoymin
            knife4j-spring-boot-starter
            3.0.2
        

2.添加配置

application.yaml文件中添加如下

springfox:
  documentation:
    swagger-ui:
      enabled: true

添加一个配置类

package com.hhmt.delivery.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import springfox.documentation.builders.*;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.*;
import springfox.documentation.spi.documentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;


@EnableKnife4j
@Configuration
@EnableOpenApi
public class Swagger3Config {

    @Bean
    public Docket createRestApi() {
        //返回文档概要信息
        return new Docket(documentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(Operation.class))
                .paths(PathSelectors.any())
                .build()
                .globalRequestParameters(getGlobalRequestParameters())
                .globalResponses(HttpMethod.GET, getGlobalResponseMessage())
                .globalResponses(HttpMethod.POST, getGlobalResponseMessage());
    }

    
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("辉煌明天第三方接入文档")
                .description("如有疑问,可联系")
                .contact(new Contact("华春", "https://hhmt-co.yoqu.net/version", "huachun_w@163.com"))
                .version("1.0")
                .build();
    }


    
    private List getGlobalRequestParameters() {
        List parameters = new ArrayList<>();
        parameters.add(new RequestParameterBuilder()
                .name("MediaDto")
                .description("请求数据")
                .required(true)
                .in(ParameterType.QUERY)
                .query(q -> q.model(m -> m.scalarModel((ScalarType.STRING))))
                .required(false)
                .build());

        return parameters;
    }

    
    private List getGlobalResponseMessage() {
        List responseList = new ArrayList<>();
        responseList.add(new ResponseBuilder().code("404").description("未找到资源").build());
        return responseList;
    }
}

3.添加注解
package com.hhmt.delivery.controller;

import com.hhmt.delivery.ocpx.bean.MediaDto;
import com.hhmt.delivery.ocpx.bean.ResultVo;
import com.hhmt.delivery.ocpx.service.common.MediaCommonService;
import com.hhmt.delivery.utils.CheckParamsUtils;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Api(tags = "第三方媒体接入接口", value = "该参数没什么意义")
@RestController(value = "第三方接入")
@RequestMapping(value = "/cpa", name = "媒体公共调用控制器")
public class OcpxCommonController {

    @Autowired
    private CheckParamsUtils checkParamsUtils;

    @Autowired
    private MediaCommonService mediaCommonService;

    @Operation(summary = "第三方上报入口")
    @PostMapping(value = "/media", name = "媒体上报")
    public ResultVo media(@Parameter(description = "媒体请求数据") @RequestBody MediaDto mediaDto) {
        checkParamsUtils.checkMediaInfo(mediaDto);
        return mediaCommonService.reportCustomer(mediaDto);
    }

}

4.访问测试

http://localhost:9527/swagger-ui/index.html

待完善... 

原文参考:https://blog.csdn.net/qq_22075913/article/details/117038037

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5697074.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存