主页:写程序的小王叔叔的博客欢迎来访👀
支持:点赞收藏关注
1、效果
2、原理
大概的内容也就是上面的一样,都是为了解决前后端分离,不浪费人工时间的前提下更新接口,参数,返回值这些的相关解释。
3、pom配置
首先是创建一个Spring Boot项目,加入web依赖,创建成功后;
增加Swagger2相关的pom依赖
<!-- Swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.8.7</version> </dependency>
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(); } }
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); } }
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; } }
5、验证结果
⚠️注意 ~
💯本期内容就结束了,如果内容有误,麻烦大家评论区指出!
如有疑问❓可以在评论区💬或私信💬,尽我最大能力🏃♀️帮大家解决👨🏫!