前后端分离ssm配置swagger接口文档

本文记录了SSM框架集成Swagger的配置过程。包括在pom.xml中添加依赖、创建SwaggerConfig.java配置类、在springmvc配置文件中添加配置、在controller层添加注解等步骤。同时提醒访问Swagger接口文档时要注意路径,否则会报404错误。

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

之前配置过springboot,相比ssm要简单很多,现在记录一下ssm的配置

在pom.xml中加入依赖

<!--swagger本身不支持spring mvc的,springfox把swagger包装了一下,让他可以支持springmvc-->
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.6.1</version>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.6.1</version>
    </dependency>

添加配置类SwaggerConfig.java

@WebAppConfiguration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages = "com.maxcore.controller")
public class SwaggerConfig {


    @Bean
    public Docket customDocket() {
        //
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("娜", "https://www.baidu.me", "baidu_666@icloud.com");
        return new ApiInfo("仿简书前台API接口",//大标题 title
                "Swagger测试demo",//小标题
                "0.0.1",//版本
                "www.baidu.com",//termsOfServiceUrl
                contact,//作者
                "Blog",//链接显示文字
                "https://www.baidu.me"//网站链接
        );
    }


}

在dispatcher-servlet.xml(springmvc的配置文件)中加入如下配置

    <bean class="com.maxcore.config.SwaggerConfig" />

    <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
    <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />

要在controller层添加注解

最后启动项目,访问swagger接口文档的路径一定要对,不然一直报404,你以为你没配置对,其实是你路径不对,笔者在这里表示有很痛的领悟

笔者的本地的访问路径是 http://localhost/jianShuSSM_war/swagger-ui.html

一般都是
http://ip地址:端口(默认80,不显示)/项目名/swagger-ui.html


github

个人网站

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值