解决springboot集成swagger碰到的坑(报404)⼀:项⽬使⽤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.fig;
import t.annotation.Bean;
import t.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;
/**
* @author baibing
* @project: springboot-mybatis
* @package: com.fig
* @Description: swagger2配置类
* @date 2018/9/25 15:35
*/
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.ller"))
.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("/licenses/by-nc-sa/4.0/")
.build();
}
}
3、在controller层添加相应的注解(@Api 和 @ApiOperation 以及 @ApiIgnore 等)
package com.ller;
import com.sailing.springbootmybatis.bean.Userinfo;
import com.sailing.springbootmybatismon.log.LogOperationEnum;
import com.sailing.springbootmybatismon.log.annotation.MyLog;
import com.sponse.BuildResponseUtil;
import com.sponse.ResponseData;
import com.sailing.springbootmybatismon.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;
/**
* @author baibing
* @project: springboot-mybatis
springboot推荐算法
* @package: com.ller
* @Description: Userinfo controller 控制层
* @date 2018/9/12 10:07
*/
@RestController
@Api(value = "USERINFO", description = "⽤户信息测试controller")
public class UserinfoController{
@Autowired
private UserinfoService userinfoService;
@Autowired
private WebSocketServer webSocketServer;
@Autowired
private RedisService redisService;
/**
* 查指定id对应的⽤户
* @param id
* @return
*/
@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);
}
/**
* 查所有⽤户
* @return
*/
@RequestMapping(value = "/users", method = RequestMethod.GET)
@MyLog(type = LogOperationEnum.SELECT,value = "查询全部⽤户信息")
@ApiOperation(value = "查询所有⽤户信息接⼝", notes = "查询所有⽤户信息接⼝")
public ResponseData getAllUsers(){
return userinfoService.findAllUsers();
}
/**
* 查所有⽤户(带分页)
* @param page 当前页数
* @param rows 每页显⽰条数
* @return
*/
@RequestMapping(value = "/users/p", method = RequestMethod.GET)
@ApiOperation(value = "查询所有⽤户信息接⼝(带分页)", notes = "查询所有⽤户信息接⼝(带分页)")
public ResponseData getAllUsers(Integer page, Integer rows){
return userinfoService.findAllUsers(page, rows);
}
/**
* 新增⽤户 (带参数校验@Valid)
* 注意:BindingResult 对象必须在 @Valid 的紧挨着的后⾯,否则接收不到错误信息
* @Valid 可以校验json 也可以校验表单传递的对象属性
* @param userinfo
* @return
*/
@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.FieldError().getDefaultMessage());
}
return userinfoService.saveUser(userinfo);
}
/**
* 删除指定⽤户
* @param id ⽤户id
* @return
*/
@RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
@ApiOperation(value = "删除指定id的⽤户信息接⼝", notes = "删除指定id的⽤户信息接⼝")
public ResponseData deleteUser(@PathVariable Integer id){
return userinfoService.deleteUser(id);
}
/**
* 更新⽤户
* @param userinfo
* @return
*/
@RequestMapping(value = "/user", method = RequestMethod.PUT)
@ApiOperation(value = "更新指定id⽤户信息接⼝", notes = "更新指定id⽤户信息接⼝")
public ResponseData updateUserinfo(@RequestBody Userinfo userinfo){
return userinfoService.updateUser(userinfo);
}
/**
* 给指定⽤户推送消息
* @param userName ⽤户名
* @param message 消息
* @throws IOException
*/
@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);
}
/**
* 测试redis接⼝保存String类型action
* @param address
* @return
*/
@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);
}
/**
* 测试redis接⼝保存实体类型action
* @param userinfo
* @return
*/
@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);
}
/**
* 测试redis接⼝读取实体类型action
* @param key
* @return
*/
@RequestMapping(value = "/redis/userinfo/{key}", method = RequestMethod.GET)
@ApiIgnore //使⽤此注解忽略⽅法的暴露,也可以⽤在controller上
@ApiOperation(value = "redis中读取指定key对应的数据接⼝", notes = "redis中读取指定key对应的数据接⼝")
public ResponseData getEntity(@PathVariable String key){
EntityValue(key);
}
/**
*
* @param list
* @return
*/
@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);
}
/**
* 测试redis接⼝读取实体类型action
* @param key
* @return
*/
@RequestMapping(value = "/redis/userList/{key}", method = RequestMethod.GET)
@ApiOperation(value = "redis中读取指定key对应的集合数据接⼝", notes = "redis中读取指定key对应的集合数据接⼝")
public ResponseData getCollection(@PathVariable String key){
CollectionValue(key);
}
}
⼆:到这⾥配置就结束了
swagger-ui界⾯展⽰
三:项⽬运⾏了⼀段时间后访问上⾯连接突然报 404 错误
百思不得其解,但是可以肯定的是加了什么配置导致,最后在l 中发现了⼀个配置:
sources.add-mapping:false
将其注释掉熟悉的界⾯⼜回来了,查阅资料发现这个配置是不⾃动给静态资源添加路径,导致swagger-ui.html不到资源,知道原因后摸索看能不能在保留以上配置的前提下⾃⼰⼿动给swagger-ui.html 添加静态资源路径呢?
package com.fig;
import t.annotation.Configuration;
import org.springframework.fig.annotation.ResourceHandlerRegistry;
import org.springframework.fig.annotation.ViewControllerRegistry;
import org.springframework.fig.annotation.WebMvcConfigurerAdapter;
/**
* 在SpringBoot2.0及Spring 5.0 WebMvcConfigurerAdapter已被废弃,⽬前到解决⽅案就有
* 1 直接实现WebMvcConfigurer (官⽅推荐)
* 2 直接继承WebMvcConfigurationSupport
* @ blog.csdn/lenkvin/article/details/79482205
*/
@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.修改端⼝号,⽬前⼀直⽤的
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。