传统spring整合swagger非springboot

本文详细介绍如何在SpringMVC项目中集成Swagger,包括配置spring-servlet.xml,使用SwaggerSpringMvcPlugin,以及设置ApiInfo等步骤,帮助快速实现API文档自动生成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.springfox-swagger2可用于springboot,用spring报错,用swagger-springmvc
2.spring版本
<spring.version>4.1.5.RELEASE</spring.version>
3.maven

<dependency>
			<groupId>com.mangofactory</groupId>
			<artifactId>swagger-springmvc</artifactId>
			<version>1.0.2</version>
		</dependency>

4.spring-servlet.xml

<!--  swagger静态文件路径 -->
	<mvc:resources mapping="/swagger/**" location="/swagger/" />
	<mvc:default-servlet-handler />
	<bean class="com.mangofactory.swagger.configuration.SpringSwaggerConfig" />
	<bean class="com.uiot.config.SwaggerConfig" />
@Configuration
@EnableSwagger
public class SwaggerConfig {
    private SpringSwaggerConfig springSwaggerConfig;

    /**
     * Required to autowire SpringSwaggerConfig
     */
    @Autowired
    public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig)
    {
        this.springSwaggerConfig = springSwaggerConfig;
    }

    /**
     * Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc
     * framework - allowing for multiple swagger groups i.e. same code base
     * multiple swagger resource listings.
     */
    @Bean
    public SwaggerSpringMvcPlugin customImplementation()
    {
        return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
                .apiInfo(apiInfo())
                .includePatterns(".*?");
    }

    private ApiInfo apiInfo()
    {
        ApiInfo apiInfo = new ApiInfo(
                "springmvc搭建swagger",
                "spring-API swagger测试",
                "My Apps API terms of service",
                "86098554@qq.com",
                "web app",
                "My Apps API License URL");
        return apiInfo;
    }
}

6.WebAppConfig swagger ui的默认路径,不用修改

@Configuration
@EnableWebMvc
public class WebAppConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

7.在GitHub上将swaggerui下载,地址:https://github.com/swagger-api/swagger-ui
也可从链接下载swagger,链接:https://pan.baidu.com/s/1LkS-a_oq58sav7cvZ7ZeiA
提取码:gz9g
放在webcontent根目录下
8.启动项目打开http://localhost:8080/swagger/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值