配置Jersey 1.x项目以生成swagger文档
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了配置Jersey 1.x项目以生成swagger文档相关的知识,希望对你有一定的参考价值。
我正在尝试配置我的Jersey 1.x REST项目来生成Swagger文档。实际上REST运行良好,但swagge不起作用。我在WAS 8.5上运行该项目,当我在URL上使用浏览器时:
http://localhost:9082/TestSwagger/swagger.yaml
服务器返回404错误。
我发布项目中包含的lib和感兴趣的java文件以获得一些帮助:
包含的库:
- 注释-2.0.1
- ASM-3.1
- 公共琅3.2.1
- 番石榴15.0
- 杰克逊的注解 - 2.4.0
- 杰克逊核心-2.4.2
- 杰克逊 - 数据绑定-2.4.2
- 杰克逊XML数据格式2.4.2
- 杰克逊数据格式2.4.2-YAML
- 杰克逊 - 数据类型 - 乔达-2.4.2
- 杰克逊JAXRS JSON的提供商 - 2.4.2
- 杰克逊模块JAXB的注释 - 2.4.2
- Javassist进行-3.18.2-GA
- 新泽西州的客户 - 1.13
- 球衣核1.13
- 新泽西州多1.13
- 球衣服务器1.13
- 新泽西州的servlet-1.13
- 碘-2,2-二时
- JSR311的API-1.1.1
- mimepull-1.6
- 反射-0.9.9
- SLF4J-API-1.6.3
- stax2-API-3.1.4
- 招摇的注解 - 1.5.0
- 招摇核-1.5.0
- 招摇-JAXRS-1.5.0
- 招摇 - 新泽西州JAXRS-1.5.0
- 招摇的模型-1.5.0
- validation-API-1.1.0.final
veb.hml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>TestSwagger</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>it.test.application.TestApplication</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.DisableWADL</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
从javax.ws.rs.core.Application扩展的类:
package it.test.application;
import io.swagger.jaxrs.config.BeanConfig;
import it.test.rest.impl.TestServiceImpl;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
public class TestApplication extends Application {
public TestApplication() {
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setHost("localhost:9082");
beanConfig.setBasePath("/TestSwagger");
beanConfig.setResourcePackage("it.test.rest.impl");
beanConfig.setScan(true);
}
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(TestServiceImpl.class);
classes.add(io.swagger.jaxrs.listing.ApiListingResource.class);
classes.add(io.swagger.jaxrs.listing.SwaggerSerializers.class);
return classes;
}
}
TestServiceImpl.java,包含REST服务的实现和swagger注释:
package it.test.rest.impl;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Contact;
import io.swagger.annotations.ExternalDocs;
import io.swagger.annotations.Info;
import io.swagger.annotations.License;
import io.swagger.annotations.SwaggerDefinition;
import io.swagger.annotations.Tag;
import it.test.bean.ReturnBean;
import it.test.rest.TestService;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@SwaggerDefinition(
info = @Info(
description = "Test",
version = "1",
title = "Test",
termsOfService = "",
contact = @Contact(
name = "Test",
email = "test@test.it",
url = "http://test"
),
license = @License(
name = "Apache 2.0",
url = "http://www.apache.org/licenses/LICENSE-2.0"
)
),
consumes = {"application/json", "application/xml"},
produces = {"application/json", "application/xml"},
schemes = {SwaggerDefinition.Scheme.HTTP},
tags = {
@Tag(name = "Private", description = "Tag used to denote operations as private")
},
externalDocs = @ExternalDocs(value = "EXT DOC", url = "http://test")
)
@Path("/test/servizi")
public class TestServiceImpl implements TestService {
@Override
@GET
@ApiOperation(value = "Get Hello",
response = TestServiceImpl.class)
@Path("/hello")
@Produces({MediaType.APPLICATION_JSON})
public ReturnBean getHello() throws Exception {
ReturnBean returnBean = new ReturnBean();
returnBean.setRitorno("HELLO");
return returnBean;
}
}
答案
正如Paul Samsotha所建议的那样,在URL中加入rest
就可以了!
以上是关于配置Jersey 1.x项目以生成swagger文档的主要内容,如果未能解决你的问题,请参考以下文章
在springboot中整合jersey和springfox-swagger2
如何将 Swagger 与 Maven + Java + Jersey +Tomcat 集成
SpringBoot2基于Swagger2生成离线Api文档