是否可以从子路径而不是应用程序上下文根提供 swagger 根?
Is it possible to serve the swagger root from a sub path as opposed to the applcation context root?
我遵循了这个例子 swagger configuration 但我想将 swagger 根(提供 swagger.json 的路径)设置为 <jersey-context-root>/api-or-some-other-path
除了无论我传递给什么config.setBasePath(some-sub-path);
swagger root 始终是 application.yml 文件中定义的 jersey app-context root,即:spring.jersey.application-path
所以看起来 basePath 是固定的。
看看你的link和代码
this.register(ApiListingResource.class);
ApiListingResource
是服务于 swagger.json
端点的实际资源 class。如果您查看 link,您可以看到 class 带有路径注释({type:json|yaml}
确定您将返回的数据类型)。
@Path("/swagger.{type:json|yaml}")
如果你想改变路径,你需要以不同的方式注册。您需要做的是使用 Resource.builder(ResourceClass)
方法获取一个构建器,我们可以在其中更改路径。例如你可以这样做。
Resource swaggerResource = Resource.builder(ApiListingResource.class)
.path("foobar/swagger.{type:json|yaml}")
.build();
然后使用 ResourceConfig#registerResource(Resource)
方法代替 ResourceConfig#register()
方法。
this.registerResource(swaggerResource);
的完整测试
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.model.Resource;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ResourceBuilderTest extends JerseyTest {
@Path("/swagger.{type:json|yaml}")
public static class ApiListingResource {
@GET
@Produces("text/plain")
public String get() {
return "Hello World!";
}
}
@Override
public ResourceConfig configure() {
Resource swaggerResource = Resource.builder(ApiListingResource.class)
.path("foobar/swagger.{type:json|yaml}")
.build();
ResourceConfig config = new ResourceConfig();
config.registerResources(swaggerResource);
return config;
}
@Test
public void testIt() {
Response response = target("foobar/swagger.json")
.request()
.get();
String data = response.readEntity(String.class);
System.out.println(data);
assertEquals("Hello World!", data);
}
}
我遵循了这个例子 swagger configuration 但我想将 swagger 根(提供 swagger.json 的路径)设置为 <jersey-context-root>/api-or-some-other-path
除了无论我传递给什么config.setBasePath(some-sub-path);
swagger root 始终是 application.yml 文件中定义的 jersey app-context root,即:spring.jersey.application-path
所以看起来 basePath 是固定的。
看看你的link和代码
this.register(ApiListingResource.class);
ApiListingResource
是服务于 swagger.json
端点的实际资源 class。如果您查看 link,您可以看到 class 带有路径注释({type:json|yaml}
确定您将返回的数据类型)。
@Path("/swagger.{type:json|yaml}")
如果你想改变路径,你需要以不同的方式注册。您需要做的是使用 Resource.builder(ResourceClass)
方法获取一个构建器,我们可以在其中更改路径。例如你可以这样做。
Resource swaggerResource = Resource.builder(ApiListingResource.class)
.path("foobar/swagger.{type:json|yaml}")
.build();
然后使用 ResourceConfig#registerResource(Resource)
方法代替 ResourceConfig#register()
方法。
this.registerResource(swaggerResource);
的完整测试
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.model.Resource;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ResourceBuilderTest extends JerseyTest {
@Path("/swagger.{type:json|yaml}")
public static class ApiListingResource {
@GET
@Produces("text/plain")
public String get() {
return "Hello World!";
}
}
@Override
public ResourceConfig configure() {
Resource swaggerResource = Resource.builder(ApiListingResource.class)
.path("foobar/swagger.{type:json|yaml}")
.build();
ResourceConfig config = new ResourceConfig();
config.registerResources(swaggerResource);
return config;
}
@Test
public void testIt() {
Response response = target("foobar/swagger.json")
.request()
.get();
String data = response.readEntity(String.class);
System.out.println(data);
assertEquals("Hello World!", data);
}
}