NutzCN Logo
问答 nutzboot swagger 如何配置 标题 扫描范围
发布于 2046天前 作者 Hamming 1970 次浏览 复制 上一个帖子 下一个帖子
标签:

nutzboot swagger 如何配置 标题 扫描范围
可以实现类似酱紫的配置吗

package com.ruoyi.framework.config;

import org.springframework.beans.factory.annotation.Autowired;
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger2的接口配置
 * 
 * @author ruoyi
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig
{
    /** 系统基础配置 */
    @Autowired
    private RuoYiConfig ruoYiConfig;

    /**
     * 创建API
     */
    @Bean
    public Docket createRestApi()
    {
        return new Docket(DocumentationType.SWAGGER_2)
                // 详细定制
                .apiInfo(apiInfo())
                .select()
                // 指定当前包路径
                .apis(RequestHandlerSelectors.basePackage("com.ruoyi.project.tool.swagger"))
                // 扫描所有 .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 添加摘要信息
     */
    private ApiInfo apiInfo()
    {
        // 用ApiInfoBuilder进行定制
        return new ApiInfoBuilder()
                .title("标题:管理系统_接口文档")
                .description("描述:用于管理集团旗下公司的人员信息,具体包括XXX,XXX模块...")
                .contact(new Contact(ruoYiConfig.getName(), null, null))
                .version("版本号:" + ruoYiConfig.getVersion())
                .build();
    }
}

6 回复

仔细看starter-swagger

这个类 创建了 标题 版本信息 还是没有

package io.nutz.nutzsite.common.config;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.boot.AppContext;
import org.nutz.boot.annotation.PropDoc;
import org.nutz.boot.starter.WebServletFace;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.resource.Scans;

import io.swagger.models.Info;
import io.swagger.models.Swagger;
import io.swagger.servlet.Reader;
import io.swagger.util.Json;

@IocBean
public class SwaggerServletStarter extends HttpServlet implements WebServletFace {
    
	private static final long serialVersionUID = 988318972932805253L;
	
	protected static final String PRE = "swagger.";
	
	@PropDoc(value = "是否启用swagger", defaultValue = "true", type = "boolean")
    public static final String PROP_ENABLE = PRE + "enable";

	@Inject
    protected PropertiesProxy conf;

    @Inject
    protected AppContext appContext;

    @Override
    public String getName() {
        return "bykj";
    }

    @Override
    public String getPathSpec() {
        return "/swagger/swagger.json";
    }

    @Override
    public Servlet getServlet() {
        if (!conf.getBoolean(PROP_ENABLE, true)) {
            return null;
        }
        return this;
    }

    @Override
    public Map<String, String> getInitParameters() {
        return new HashMap<>();
    }
    
    @IocBean(name="swagger")
    public Swagger createSwagger() {
        return conf.makeDeep(Swagger.class, "swagger.conf.");
    }
    
    @IocBean(name="swaggerInfo")
    public Info createSwaggerInfo() {
        return conf.makeDeep(Info.class, "swagger.info.");
    }
    

    @Override
    public void init(ServletConfig config) throws ServletException {
        Ioc ioc = appContext.getIoc();
        Swagger swagger = ioc.get(Swagger.class);
        swagger.setInfo(ioc.get(Info.class, "swaggerInfo"));
        HashSet<Class<?>> classes = new HashSet<>();
        String pkgName = conf.get("swagger.resource.package", appContext.getPackage());
        for (Class<?> klass : Scans.me().scanPackage(pkgName)) {
            classes.add(klass);
        }
        Reader.read(swagger, classes);
        config.getServletContext().setAttribute("swagger", swagger);
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        final String pathInfo = request.getRequestURI();
        if (pathInfo.endsWith("/swagger.json")) {
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().println(Json.mapper().writeValueAsString(request.getServletContext().getAttribute("swagger")));
        } else {
            response.setStatus(404);
        }
    }

}

仔细看demo

配置swagger.info.XXX 就行了

添加回复
请先登陆
回到顶部