主页:​小王叔叔的博客​​欢迎来访

支持:点赞SpringBoot增加Swagger_SpringBootSpringBoot增加Swagger_SpringBoot_02​收藏SpringBoot增加Swagger_SpringBoot_03关注SpringBoot增加Swagger_Swagger_04

1、效果

SpringBoot增加Swagger_微服务_05SpringBoot增加Swagger_Swagger_06

2、原理

SpringBoot增加Swagger_SpringBoot_07SpringBoot增加Swagger_Swagger_08

 SpringBoot增加Swagger_SpringMVC_09

大概的内容也就是上面的一样,都是为了解决前后端分离,不浪费人工时间的前提下更新接口,参数,返回值这些的相关解释。

3、pom配置

首先是创建一个Spring Boot项目,加入web依赖,创建成功后;

增加Swagger2相关的pom依赖

<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
<version>2.4.0version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
<version>2.4.0version>
dependency>
<dependency>
<groupId>com.github.xiaoymingroupId>
<artifactId>swagger-bootstrap-uiartifactId>
<version>1.8.7version>
dependency>

SpringBoot增加Swagger_SpringMVC_10

4、基础swagger配置

4.1新建Swagger配置类

/**
* Swagger设置类
*/
@Configuration
//@ComponentScan(basePackages = {"com.personal.pserver.platform.*.controller"})
//配置controller路径下面有,这个路径是包名
@EnableSwagger2
public class Swagger2 {

@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.enable(true)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.personal.pserver"))
//apis的注释:注释后,可以显示接口名,具体待研究原因
// .apis(RequestHandlerSelectors.withClassAnnotation(ApiOperation.class))
// .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build().groupName("后台管理").pathMapping("/");
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("[Person-server]").description("说明[后台管理]").version("1.0").build();
}
}

SpringBoot增加Swagger_微服务_11

4.2增加静态请求拦截WebMvcConfigurerAdapter

@Configuration
public class WebMVCConfiguration extends WebMvcConfigurerAdapter {



@Override
public void addInterceptors(InterceptorRegistry registry) {

}

/**
* 添加静态资源--过滤
* @param registry
*/
@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/");

super.addResourceHandlers(registry);
}



}

SpringBoot增加Swagger_SpringBoot_12

4.3业务代码中添加api注解

entity中添加

///------------lobok
@Data//注解在类上,相当于同时使用了@Setter+@Getter+@EqualsAndHashCode+@NoArgsConstructor+@ToString
@Builder
@AllArgsConstructor//注解在类上,自动生成全部参数构造方法
@NoArgsConstructor//注解在类上,自动生成空参构造方法
////--------------swagger
@Entity
@Table(name="p_platform")
@TypeAlias("pplatform")
@ApiModel(value = "PlatformEntity", description = "平台基本信息")
public class PPlatformEntity implements Serializable {


@Id
@NonNull
@ApiModelProperty(value = "主键id", dataType = "String")
private String id;

@ApiModelProperty(value = "平台基本名称", dataType = "String")
private String platform_name;

@ApiModelProperty(value = "基本描述", dataType = "String")
private String platform_desc;
}

4.4 controller注解配置

@RestController
@Api(tags = "平台基本信息管理")
@RequestMapping("/v1/pserver/platform/manager")
public class PlatformController {

@Autowired
private PPlatformService platformService;

@ApiOperation(value = "获取平台基本信息", notes = "获取平台基本信息", httpMethod = "GET")
@GetMapping(value = "/findPlatformInfo")
public PPlatformEntity findPlatformInfo(@Param("id") String id) {
PPlatformEntity pPlatform = platformService.findOnePlatformById(id);
return pPlatform;
}
}

SpringBoot增加Swagger_SpringMVC_13

5、验证结果

SpringBoot增加Swagger_微服务_14SpringBoot增加Swagger_微服务_15


转载声明:本文为博主原创文章,未经博主允许不得转载。

⚠️注意 ~

💯本期内容就结束了,如果内容有误,麻烦大家评论区指出

如有疑问❓可以在评论区💬或私信💬,尽我最大能力🏃‍♀️帮大家解决👨‍🏫!

如果我的文章有帮助,欢迎点赞+关注✔️鼓励博主🏃,您的鼓励是我分享的动力🏃🏃🏃~