1.jar包
2.配置
3.controller
先从jar包说起 添加如下jar包
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
<exclusions>
<exclusion>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
配置
import springfox.documentation.spi.DocumentationType;
@Configuration //注入spring容器
@EnableSwagger2 //开启swagger
public class Swagger2 {
@Bean
public Docket createRestApi() {
/*
//Swagger中添加请求头
ParameterBuilder userNamePar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<Parameter>();
userNamePar.name("x_user_name").description("登陆用户名!!!")
.modelRef(new ModelRef("string")).parameterType("header")
.required(false).build(); //header中的x_user_name参数非必填,传空也可以
pars.add(userNamePar.build()); //根据每个方法名也知道当前方法在设置什么参数
*/
List<Parameter> pars = new ArrayList<Parameter>();
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.quan.controller"))
.paths(PathSelectors.any()).build()
.globalOperationParameters(pars);
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.××××.controller"))
.paths(PathSelectors.any()).build()
.globalOperationParameters(pars);
}
@SuppressWarnings("deprecation")
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Demo RESTful APIs")
.description("Demo RESTful APIs")
.termsOfServiceUrl("")
.contact("").version("0.0.1-SNAPSHOT").build();
}
}
controller
@CrossOrigin(origins = "*") //解决跨域
@RefreshScope
@RestController
//@Api(tags = {"Module"})
@Api(description = "消息模块", tags = "news")
@RequestMapping("/Manager/Module")
public class ModuleManager {
private Logger logger = LoggerFactory.getLogger(ModuleManager.class);
@Autowired
private SysModuleService sysModuleService;
@ApiOperation(value="获取父级模块",notes="通过该接口获取父级模块,获取PId=0的模块")
@GetMapping("/Parents")
public ResponseResult getModuleParents
(@RequestParam(value = "page_no",required = false) Integer page_no, @RequestParam(value = "page_size",required = false)
Integer page_size) {
logger.info("getModuleParents()");
return null;
}
}
1.有时候会遇到swagger 不显示接口的情况,注意在swagger2的配置中
.apis(RequestHandlerSelectors.basePackage("com.××××.controller"))
这里要写上controller的全限定名。
2.有时候又会遇到点击接口没有反应的情况,注意在
@Api(description = "首页图片",tags = "homepage")
public class SysNewsController {
controller层的Api注解上的tags属性的值写成英文即可。
还有一个小问题 需要提出
当controller层没有返回数据时
public ServerResponse add(@RequestParam(name = "sysNews", required = false) String sysNews,
@RequestParam(name = "id", required = false) Integer id) {
/**
* 接口响应的封装类
*
*/
public class ServerResponse implements Serializable {
private static final long serialVersionUID = 8095809201605680157L;
private int code;
private String msg;
private Object data;
当controller层有返回数据时
public Response<List<SysNews>> add(@RequestParam(name = "sysNews", required = false) String sysNews,
@RequestParam(name = "id", required = false) Integer id) {
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public class Response<T> {
/**
* 是否成功,true-成功,false-失败
*/
private Boolean success;
/**
* 状态码
*/
private int code;
/**
* 状态描述信息
*/
private String message;
/**
* 业务数据
*/
private T data;
这辈子坚持与不坚持都不可怕,怕的是独自走在坚持的道路上!
欢迎加入技术群聊!