cxf(2.7.18) spring(3.2.7.RELEASE) struts2(2.3.36).如果使用高于3.2.7.RELEASE的,在struts序列化json时会提示TruePointcut无法序列化的错误

一、使用io.springfox(2.0.3)。

参考文档:http://springfox.github.io/springfox/docs/current/#configuring-springfox-staticdocs。

https://stackoverflow.com/questions/26720090/a-simple-way-to-implement-swagger-in-a-spring-mvc-application

跟spring mvc集成,建议使用spring3.2及以上。自动扫描识别@RequestMapping标注的类和接口,生成接口文档。没有使用RequestMapping标注的将不会生成。 

根据@Api生成接口分组及描述。

根据@RequestMapping生成接口,根据@ApiOperation生产接口描述

二、配置

增加配置类

@EnableWebMvc
@EnableSwagger2
public class ApplicationSwaggerConfig {
	@Bean
    public Docket customTest1Docket() {
        return new Docket(DocumentationType.SWAGGER_12).groupName("test1")
                .apiInfo(apiInfo());
    }
	
	@Bean
	 public Docket customTest2Docket() {
	        return new Docket(DocumentationType.SWAGGER_2).groupName("test2")
	                .apiInfo(apiInfo());
	    }
	 
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("REST API 接口")
                .description("REST API接口")
                .contact("www.wsncm.com")
                .version("1.1.0")
                .build();
    }
}

pom.xml增加

		<dependency>
		    <groupId>io.springfox</groupId>
		    <artifactId>springfox-swagger2</artifactId>
		    <version>2.0.3</version>
		</dependency>
		
		<dependency>
		    <groupId>io.springfox</groupId>
		    <artifactId>springfox-swagger-ui</artifactId>
		    <version>2.0.3</version>
		</dependency>

web.xml增加

	<servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/applicationContextmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>/configuration/security</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>/configuration/ui</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>/swagger-resources</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>/v2/api-docs</url-pattern>
	</servlet-mapping>

增加web配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
			http://www.springframework.org/schema/beans
			http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
			http://www.springframework.org/schema/context
			http://www.springframework.org/schema/context/spring-context-3.0.xsd
			http://www.springframework.org/schema/mvc 
			http://www.springframework.org/schema/mvc/spring-mvc.xsd">
     
	<context:component-scan base-package="com.wsn.swagger,com.wsn.deviceIO.lib.rest,springfox.*"/>
    <!-- Enables swgger ui-->
    <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
    <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

    <!-- Include a swagger configuration-->
    <bean name="applicationSwaggerConfig" class="com.wsn.swagger.ApplicationSwaggerConfig"/>
</beans>

编写对应的接口并注解.

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐