當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Swagger UI 与SpringMVC的整合 II
生活随笔
收集整理的這篇文章主要介紹了
Swagger UI 与SpringMVC的整合 II
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
pom.xml
<!-- swagger開始 --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.6.1</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.6.1</version></dependency><dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.2.4.Final</version> </dependency><!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api --><dependency><groupId>javax.ws.rs</groupId><artifactId>javax.ws.rs-api</artifactId><version>2.0</version></dependency><!-- swagger結束 --><!-- jackson start --><dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.jr</groupId> <artifactId>jackson-jr-all</artifactId> <version>2.5.0</version> </dependency> <!-- jackson end -->spring-mvc.xml
<!-- 自動掃描(自動注入) --><mvc:annotation-driven /><context:component-scan base-package="com.qdingnet.pcloud.api"/><mvc:default-servlet-handler/><mvc:resources mapping="swagger/**" location="swagger/"/><mvc:resources mapping="webjars/**" location="classpath:/META-INF/resources/webjars/"/><bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"><property name="favorPathExtension" value="true" /><property name="favorParameter" value="true" /><property name="parameterName" value="format" /><property name="ignoreAcceptHeader" value="false" /><property name="mediaTypes"><value>json=application/json</value></property></bean><bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"><!-- 內容協商管理器 用于決定media type --><property name="contentNegotiationManager" ref="contentNegotiationManager" /><!-- 根據mediaTypes增加相應的解析器 --><property name="defaultViews"><list><bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" /></list></property></bean><bean id="stringHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>text/plain;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> <bean id="jackson2HttpMessageConverter"class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"><property name="objectMapper"><bean class="com.fasterxml.jackson.databind.ObjectMapper"><property name="dateFormat"><bean class="java.text.SimpleDateFormat"><constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"/></bean></property></bean></property></bean><mvc:annotation-driven><mvc:message-converters><ref bean="jackson2HttpMessageConverter"/><ref bean="stringHttpMessageConverter"/></mvc:message-converters></mvc:annotation-driven>SwaggerConfig.java
@EnableWebMvc @EnableSwagger2 @ComponentScan(basePackages = {"com.qdingnet.pcloud.api.swagger"}) @Configuration public class SwaggerConfig extends WebMvcConfigurerAdapter {@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.qdingnet.pcloud.api.swagger")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("Spring 中使用Swagger2構建RESTful APIs").termsOfServiceUrl("http://localhost:8080/v2/api-docs").contact("my swagger").version("1.0.0").build();} }?
具體其他細節可以查看:
Swagger UI 與SpringMVC的整合
總結
以上是生活随笔為你收集整理的Swagger UI 与SpringMVC的整合 II的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android下添加新的自定义键值和按键
- 下一篇: eclipse替换空格和注释