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 { @Bean public 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(); } }
具体其他细节可以查看: