上一个文章已经开发接口功能
pom.xml依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
再给一下完整的
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
修改启动类 在原先的基础上增加
@EnableSwagger2
package com.study;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author
* @create 2022-10-28 10:14
*/
@SpringBootApplication
@EnableSwagger2
public class StudyApplication {
public static void main(String[] args) {
SpringApplication.run(StudyApplication.class,args);
}
}
创建 SwaggerConfiguration 类
这里我们采用了groupName 可以实现不同开发者,快速找到自己开发的接口,功能可能实际没什么大的作用,但是这里尝试了下。
package com.study.Configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
/**
* @author
* @create 2022-10-27 17:41
*/
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
private static ApiInfo DEFAULT = null;
@Bean //在方法上打上注解 @Bean即表示声明该方法返回的实例是受 Spring 管理的 Bean
public Docket docket(Environment environment){
Contact DEFAULT_CONTACT = new Contact("AAA", "http://www.baidu.com", "aaaaaaaaaa@qq.com");
DEFAULT = new ApiInfo("AAA的开发接口",
"Api Documentation",
"V-1.0",
"http://www.baidu.com",
DEFAULT_CONTACT,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
// Profiles profiles = Profiles.of("dev");//设置在那个环境下显示swagger
// boolean b = environment.acceptsProfiles(profiles);//获得项目的环境
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(DEFAULT)
.groupName("AAA")
// .enable(b)//配置swagger是否开启,如果为false则关闭swagger,默认为true
.select()
.apis(RequestHandlerSelectors.basePackage("com.study.controller2"))
.build();
}
@Bean //在方法上打上注解 @Bean即表示声明该方法返回的实例是受 Spring 管理的 Bean
public Docket docket1(Environment environment){
Contact DEFAULT_CONTACT = new Contact("BBB", "http://www.baidu.com", "bbbbbbbbb@qq.com");
DEFAULT = new ApiInfo("BBB的开发接口",
"Api Documentation",
"V-1.0",
"http://www.baidu.com",
DEFAULT_CONTACT,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
// Profiles profiles = Profiles.of("dev");//设置在那个环境下显示swagger
// boolean b = environment.acceptsProfiles(profiles);//获得项目的环境
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(DEFAULT)
.groupName("BBB")
// .enable(b)//配置swagger是否开启,如果为false则关闭swagger,默认为true
.select()
.build();
}
}
再加一个 WebMvcConfigurer 没有这个可能报错,有不佳的方式这里是写了
package com.study.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
* @author
* @create 2022-10-28 11:55
*/
public class WebMvcConfigurer extends WebMvcConfigurationSupport {
/**
* 发现如果继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。 需要重新指定静态资源
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/static/");
registry.addResourceHandler("swagger-ui.html", "doc.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}
再resource 下增加 application.yml
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
然后在controller 上增加信息
这里放下我的目录结构
第一个
package com.study.controller;
import com.study.model.RespResult;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author
* @create 2022-10-27 17:36
*/
@Api(tags="用户模块")
@Controller
@RequestMapping(value = "/user")
public class UserController {
@ApiOperation("获取用户信息")
@ApiImplicitParams({
@ApiImplicitParam(paramType="query", name="userId", dataType="String", required=true, value="用户 Id")
})
@ApiResponses({
@ApiResponse(code = 400, message = "请求参数没填好"),
@ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
})
@ResponseBody
@RequestMapping("/list")
public RespResult list() {
return RespResult.ok().put("page", "12");
}
}
第二个
package com.study.controller2;
import com.study.model.RespResult;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author
* @create 2022-10-27 17:36
*/
@Api(tags="用户模块2")
@Controller
@RequestMapping(value = "/user2")
public class UserController2 {
@ApiOperation("获取用户信息")
@ApiImplicitParams({
@ApiImplicitParam(paramType="query", name="userId", dataType="String", required=true, value="用户 Id")
})
@ApiResponses({
@ApiResponse(code = 400, message = "请求参数没填好"),
@ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
})
@ResponseBody
@RequestMapping("/list2")
public RespResult list2() {
return RespResult.ok().put("page", "12");
}
@ApiOperation("第一个接口")
@ApiResponses({
@ApiResponse(code = 400, message = "请求参数没填好"),
@ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
})
@ResponseBody
@RequestMapping("/index")
public String helloword(){
return "第一个接口";
}
}
最后写个
package com.study.model;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public class RespResult extends HashMap<String, Object> implements Serializable {
private static final long serialVersionUID = 7016667591434008789L;
public RespResult() {
this.put((String) "code", Integer.valueOf(200));
this.put((String) "msg", "success");
}
public static RespResult error() {
return error(500, "未知异常,请联系管理员");
}
public static RespResult validError() {
return error(500, "参数不合法");
}
public static RespResult error(String msg) {
return error(500, msg);
}
public static RespResult error(int code, String msg) {
RespResult r = new RespResult();
r.put((String) "code", Integer.valueOf(code));
r.put((String) "msg", msg);
return r;
}
public static RespResult ok(int code, String msg) {
RespResult r = new RespResult();
r.put((String) "code", Integer.valueOf(code));
r.put((String) "msg", msg);
return r;
}
public static RespResult ok(String msg) {
RespResult r = new RespResult();
r.put((String) "msg", msg);
return r;
}
public static RespResult ok(Map<String, Object> map) {
RespResult r = new RespResult();
r.putAll(map);
return r;
}
public static RespResult ok() {
return new RespResult();
}
@Override
public RespResult put(String key, Object value) {
super.put(key, value);
return this;
}
}
ok 启动
访问 http://localhost:8080/swagger-ui.html
切换用户