iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >解决springboot集成swagger碰到的坑(报404)
  • 351
分享到

解决springboot集成swagger碰到的坑(报404)

2024-04-02 19:04:59 351人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

一:项目使用SpringBoot集成swagger进行调试 配置swagger非常简单,主要有三步: 1、添加swagger依赖 <!-- 引入 swagger等相关依赖

一:项目使用SpringBoot集成swagger进行调试

配置swagger非常简单,主要有三步:

1、添加swagger依赖


<!-- 引入 swagger等相关依赖 -->
<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>

2、进行swagger的配置


package com.sailing.springbootmybatis.config;
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 Swagger2Config {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.sailing.springbootmybatis.controller"))
                .paths(PathSelectors.any())
                .build();
    }
 
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("SPRING-BOOT整合MYBATIS--API说明文档")
                .description("2018-8完成版本")
                .contact("Sailing西安研发中心-baibing")
                .version("1.0")
                .license("署名-非商用-相同方式共享 4.0转载请保留原文链接及作者")
                .licenseUrl("https://creativecommons.org/licenses/by-nc-sa/4.0/")
                .build();
    }
}

3、在controller层添加相应的注解(@Api 和 @ApiOperation 以及 @ApiIgnore 等)


package com.sailing.springbootmybatis.controller;
import com.sailing.springbootmybatis.bean.Userinfo;
import com.sailing.springbootmybatis.common.log.LoGoperationEnum;
import com.sailing.springbootmybatis.common.log.annotation.MyLog;
import com.sailing.springbootmybatis.common.response.BuildResponseUtil;
import com.sailing.springbootmybatis.common.response.ResponseData;
import com.sailing.springbootmybatis.common.websocket.WebSocketServer;
import com.sailing.springbootmybatis.service.RedisService;
import com.sailing.springbootmybatis.service.UserinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
 

@RestController
@Api(value = "USERINFO", description = "用户信息测试controller")
public class UserinfoController{
    @Autowired
    private UserinfoService userinfoService;
    @Autowired
    private WebSocketServer webSocketServer;
    @Autowired
    private RedisService redisService;
    
    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
    @MyLog(type = LogOperationEnum.SELECT,value = "查询指定id的用户信息")
    @ApiOperation(value = "查询指定id的用户信息接口", notes = "查询指定id的用户信息接口")
    public ResponseData getUser(@PathVariable(value = "id") Integer id){
        return userinfoService.findById(id);
    }
    
    @RequestMapping(value = "/users", method = RequestMethod.GET)
    @MyLog(type = LogOperationEnum.SELECT,value = "查询全部用户信息")
    @ApiOperation(value = "查询所有用户信息接口", notes = "查询所有用户信息接口")
    public ResponseData getAllUsers(){
        return userinfoService.findAllUsers();
    }
 
    
    @RequestMapping(value = "/users/p", method = RequestMethod.GET)
    @ApiOperation(value = "查询所有用户信息接口(带分页)", notes = "查询所有用户信息接口(带分页)")
    public ResponseData getAllUsers(Integer page, Integer rows){
        return userinfoService.findAllUsers(page, rows);
    }
 
    
    @RequestMapping(value = "/user", method = RequestMethod.POST)
    @MyLog(type = LogOperationEnum.INSERT, value = "新增用户信息")
    @ApiOperation(value = "新增用户接口(包含参数校验)", notes = "新增用户接口(包含参数校验)")
    public ResponseData saveUserinfo(@RequestBody @Valid Userinfo userinfo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return BuildResponseUtil.buildFailResponse(bindingResult.getFieldError().getDefaultMessage());
        }
        return userinfoService.saveUser(userinfo);
    }
 
    
    @RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除指定id的用户信息接口", notes = "删除指定id的用户信息接口")
    public ResponseData deleteUser(@PathVariable Integer id){
        return userinfoService.deleteUser(id);
    }
 
    
    @RequestMapping(value = "/user", method = RequestMethod.PUT)
    @ApiOperation(value = "更新指定id用户信息接口", notes = "更新指定id用户信息接口")
    public ResponseData updateUserinfo(@RequestBody Userinfo userinfo){
        return userinfoService.updateUser(userinfo);
    }
 
    
    @RequestMapping(value = "/socket", method = RequestMethod.GET)
    @ApiIgnore //使用此注解忽略方法的暴露,也可以用在controller上
    @ApiOperation(value = "给指定用户推送socket消息接口", notes = "给指定用户推送socket消息接口")
    public void testSocket(@RequestParam String userName,@RequestParam String message){
        webSocketServer.sendInfo(userName, message);
    }
 
    
    @RequestMapping(value = "/redis", method = RequestMethod.POST)
    @ApiIgnore //使用此注解忽略方法的暴露,也可以用在controller上
    @ApiOperation(value = "redis中添加String数据接口", notes = "redis中添加String数据接口")
    public ResponseData setString(@RequestBody String address){
        System.out.println(address);
        return redisService.setValue(address);
    }
 
    
    @RequestMapping(value = "/redis/userinfo", method = RequestMethod.POST)
    @ApiIgnore //使用此注解忽略方法的暴露,也可以用在controller上
    @ApiOperation(value = "redis中添加Userinfo实体接口", notes = "redis中添加Userinfo实体接口")
    public ResponseData setEntity(@RequestBody Userinfo userinfo){
        return redisService.setEntityValue(userinfo);
    }
    
    @RequestMapping(value = "/redis/userinfo/{key}", method = RequestMethod.GET)
    @ApiIgnore //使用此注解忽略方法的暴露,也可以用在controller上
    @ApiOperation(value = "redis中读取指定key对应的数据接口", notes = "redis中读取指定key对应的数据接口")
    public ResponseData getEntity(@PathVariable String key){
        return redisService.getEntityValue(key);
    }
 
    
    @RequestMapping(value = "/redis/userList", method = RequestMethod.POST)
    @ApiIgnore //使用此注解忽略方法的暴露,也可以用在controller上
    @ApiOperation(value = "redis中添加包含Userinfo实体的集合接口", notes = "redis中添加包含Userinfo实体的集合接口")
    public ResponseData setCollection(@RequestBody List<Userinfo> list){
        return redisService.setCollectionValue(list);
    }
 
    
    @RequestMapping(value = "/redis/userList/{key}", method = RequestMethod.GET)
    @ApiOperation(value = "redis中读取指定key对应的集合数据接口", notes = "redis中读取指定key对应的集合数据接口")
    public ResponseData getCollection(@PathVariable String key){
        return redisService.getCollectionValue(key);
    }
}

二:到这里配置就结束了

访问 Http://127.0.0.1:端口/项目路径/swagger-ui.html 就ok了, 页面如下:

swagger-ui界面展示

三:项目运行了一段时间后访问上面连接突然报 404 错误

百思不得其解,但是可以肯定的是加了什么配置导致,最后在application.yml 中发现了一个配置:


spring.mvv.resources.add-mapping:false

将其注释掉熟悉的界面又回来了,查阅资料发现这个配置是不自动给静态资源添加路径,导致swagger-ui.html找不到资源,知道原因后摸索看能不能在保留以上配置的前提下自己手动给swagger-ui.html添加静态资源路径呢?


package com.sailing.springbootmybatis.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerReGIStry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebmvcConfigurerAdapter;
 

@Configuration
public class WebMvcConfig 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/");
    }
}

发现通过以上代码手动给swagger-ui.html指定路径也可以解决404的问题。

Springboot集成Swagger遇到无限死循环

解决方法

1.万能办法,重启,我自己用好使

2.同事说的方法,因重启无效,断网一会

3.修改端口号,目前一直用的

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: 解决springboot集成swagger碰到的坑(报404)

本文链接: https://www.lsjlt.com/news/128094.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作