Jersey2+swagger组建restful风格api及文档管理
1.jar包引入
<dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-server</artifactId> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-jersey2-jaxrs</artifactId> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> </dependency> <dependency> <groupId>org.glassfish.jersey.inject</groupId> <artifactId>jersey-hk2</artifactId> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> </dependency>
2.初始化Jersey、swagger
2.1 方式一 在web.xml中配置
初始化Jersey
<servlet> <servlet-name>Jersey REST Service</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>io.swagger.jaxrs.listing,com.baozh.api</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey REST Service</servlet-name> <url-pattern>/api/*</url-pattern> </servlet-mapping>
初始化swagger,初始化url、版本信息、根目录,会提示“缺少mapping”错误,可以忽略
<servlet> <servlet-name>Jersey2Config</servlet-name> <servlet-class>io.swagger.jersey.config.JerseyJaxrsConfig</servlet-class> <init-param> <param-name>api.version</param-name> <param-value>1.0.0</param-value> </init-param> <init-param> <param-name>swagger.api.basepath</param-name> <param-value>http://localhost:8087/rest/api</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet>
2.2 方式二,通过application类+web.xml初始化swagger与Jersey
web.xml中servlet配置
<servlet> <servlet-name>Jersey REST Service</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.Jersey.config.SampleApplication</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey REST Service</servlet-name> <url-pattern>/api/*</url-pattern> </servlet-mapping>
application类配置
public class SampleApplication extends Application { public SampleApplication() { BeanConfig beanConfig = new BeanConfig(); beanConfig.setVersion("1.0.2"); beanConfig.setSchemes(new String[]{"http"}); beanConfig.setHost("localhost:8087"); beanConfig.setBasePath("/rest/api"); beanConfig.setResourcePackage("com.jersey.api"); beanConfig.setScan(true); } @Override public Set<Class<?>> getClasses() { Set<Class<?>> resources = new HashSet(); //api入口 resources.add(ApiService.class); //json转换 resources.add(JacksonFeature.class); resources.add(io.swagger.jaxrs.listing.ApiListingResource.class); resources.add(io.swagger.jaxrs.listing.SwaggerSerializers.class); return resources; } }
3.加入swagger ui
将 https://github.com/swagger-api/swagger-ui中dist目录中的文件拷贝至项目api-docs目录中
拷贝ui文件并修改index.html 文件中url信息为工程的实际地址
访问http://localhost:8087/rest/api-docs即可看到swagger-ui信息
api请求效果
参考资料
https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5