SpringMVC整合kinfe4j及問(wèn)題解決分析
引言
以前的SpringMVC代碼都是使用showdoc來(lái)記錄接口文檔,正好這次開(kāi)啟一個(gè)新項(xiàng)目,準(zhǔn)備整合進(jìn)swagger,以免編寫接口文檔的痛苦。而前段時(shí)間又正好看到很多人使用kinfe4j來(lái)替換swagger的文章,索性一步到位用上了kinfe4j。
整合的步驟
現(xiàn)在的項(xiàng)目都是用Spring Boot框架的比較多,而Spring Boot整合kinfe4j網(wǎng)上的文章也比較多,而且相對(duì)比較容易,而隨著時(shí)間的推移,SpringMVC用的越來(lái)越少,相對(duì)而言資料也越來(lái)越少。這次整合也耗費(fèi)了我半天的時(shí)間,下面記錄一下整合的步驟及需要注意的問(wèn)題。
首先導(dǎo)入maven
<dependency> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> <version>1.5.21</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> <exclusions> <exclusion> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.3</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-bean-validators</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.1.0.Final</version> </dependency>
第二步,編寫config文件:
@EnableSwagger2 @EnableSwaggerBootstrapUI @Import(BeanValidatorPluginsConfiguration.class) @EnableWebMvc public class SwaggerConfig extends WebMvcConfigurerAdapter { public Docket defaultApi(){ Docket docket = new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .groupName("2.x 版本") .select() .apis(RequestHandlerSelectors.basePackage("com.ruida.cloud.sys.controller")) .paths(PathSelectors.any()) .build(); return docket; } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("管理后臺(tái)接口文檔") .description("管理后臺(tái)接口文檔") .termsOfServiceUrl("http://localhost:8090/swagger/doc.html") .version("1.0.0") .build(); } }
這些配置都比較簡(jiǎn)單。
第三步spring-mvc.xml文件中,進(jìn)行bean的注冊(cè):
<bean id="SwaggerConfig" class="xxx.sys.controller.config.SwaggerConfig" /> 接下來(lái)在web.xml中進(jìn)行配置: ```xml <filter> <filter-name>swaggerProductionFilter</filter-name> <filter-class>com.github.xiaoymin.swaggerbootstrapui.filter.ProductionSecurityFilter</filter-class> <init-param> <param-name>production</param-name> <param-value>false</param-value> </init-param> </filter> <filter-mapping> <filter-name>swaggerProductionFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--Swagger資源的Basic認(rèn)證保護(hù)策略--> <filter> <filter-name>swaggerSecurityBasic</filter-name> <filter-class>com.github.xiaoymin.swaggerbootstrapui.filter.SecurityBasicAuthFilter</filter-class> <!--開(kāi)啟basic認(rèn)證--> <init-param> <param-name>enableBasicAuth</param-name> <param-value>true</param-value> </init-param> <!--用戶名&密碼--> <init-param> <param-name>userName</param-name> <param-value>123</param-value> </init-param> <init-param> <param-name>password</param-name> <param-value>123</param-value> </init-param> </filter> <filter-mapping> <filter-name>swaggerSecurityBasic</filter-name> <url-pattern>/swagger/*</url-pattern> </filter-mapping>
這個(gè)時(shí)候啟動(dòng),如果訪問(wèn)xxx/doc.html顯示404,需要配置靜態(tài)資源映射路徑,這邊在config里面一并完成,繼承WebMvcConfigurerAdapter,重寫下面的方法:
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); }
還有一點(diǎn)需要注意的:如果配置了shiro等權(quán)限的話,需要將kinfe4j用到的靜態(tài)文件權(quán)限放開(kāi):
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <property name="loginUrl" value="/login"/> <property name="successUrl" value="/"/> <property name="unauthorizedUrl" value="/login"/> <property name="filterChainDefinitions"> <value> /captcha = anon /login = authc /logout = logout /uploadFile = user /swagger-ui.html = anon /doc.html = anon /swagger-resources/** = anon /v2/** = anon /static/** = anon /webjars/** = anon /** = user </value> </property> <property name="filters"> <map> <entry key="authc" value-ref="formAuthenticationFilter"/> </map> </property> </bean>
其他類似的權(quán)限校驗(yàn)的組件也是同樣的操作,不再贅述。至此,整合完成。
在實(shí)體類,和接口類配置好相關(guān)的注解后,訪問(wèn):
以上就是SpringMVC整合kinfe4j及問(wèn)題解決分析的詳細(xì)內(nèi)容,更多關(guān)于SpringMVC整合kinfe4j的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java性能優(yōu)化之關(guān)于大對(duì)象復(fù)用的目標(biāo)和注意點(diǎn)
這篇文章主要介紹了Java性能優(yōu)化之關(guān)于大對(duì)象復(fù)用的目標(biāo)和注意點(diǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03基于jenkins構(gòu)建結(jié)果企業(yè)微信提醒
這篇文章主要介紹了基于jenkins構(gòu)建結(jié)果企業(yè)微信提醒,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08Java實(shí)現(xiàn)文件分片上傳接口的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Java語(yǔ)言實(shí)現(xiàn)文件分片上傳的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2022-07-07Java實(shí)現(xiàn)定時(shí)任務(wù)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)定時(shí)任務(wù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09Springboot中用 Netty 開(kāi)啟UDP服務(wù)方式
這篇文章主要介紹了Springboot中用 Netty 開(kāi)啟UDP服務(wù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11SpringBoot的application.yml不生效問(wèn)題及解決
這篇文章主要介紹了SpringBoot的application.yml不生效問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03