Is it possible to serve the swagger root from a sub path as opposed to the applcation context root?
I followed this example swagger configuration but would like to set the swagger root (the path with which the swagger.json is served) to <jersey-context-root>/api-or-some-other-path
except that no matter what I pass to the config.setBasePath(some-sub-path);
the swagger root is always the jersey app-context root defined in the application.yml file, i.e: spring.jersey.application-path
so it seems the basePath is hard-wired.
spring-boot jersey-2.0 swagger-2.0
add a comment |
I followed this example swagger configuration but would like to set the swagger root (the path with which the swagger.json is served) to <jersey-context-root>/api-or-some-other-path
except that no matter what I pass to the config.setBasePath(some-sub-path);
the swagger root is always the jersey app-context root defined in the application.yml file, i.e: spring.jersey.application-path
so it seems the basePath is hard-wired.
spring-boot jersey-2.0 swagger-2.0
add a comment |
I followed this example swagger configuration but would like to set the swagger root (the path with which the swagger.json is served) to <jersey-context-root>/api-or-some-other-path
except that no matter what I pass to the config.setBasePath(some-sub-path);
the swagger root is always the jersey app-context root defined in the application.yml file, i.e: spring.jersey.application-path
so it seems the basePath is hard-wired.
spring-boot jersey-2.0 swagger-2.0
I followed this example swagger configuration but would like to set the swagger root (the path with which the swagger.json is served) to <jersey-context-root>/api-or-some-other-path
except that no matter what I pass to the config.setBasePath(some-sub-path);
the swagger root is always the jersey app-context root defined in the application.yml file, i.e: spring.jersey.application-path
so it seems the basePath is hard-wired.
spring-boot jersey-2.0 swagger-2.0
spring-boot jersey-2.0 swagger-2.0
asked Nov 19 '18 at 14:10


Dark Star1
2,902105595
2,902105595
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Look at your link and the code
this.register(ApiListingResource.class);
That ApiListingResource
is the actual resource class that serves up the swagger.json
endpoint. If you look at the link, you can see the class is annotated with the path (the {type:json|yaml}
determines what type if data you will get back).
@Path("/swagger.{type:json|yaml}")
If you want to change the path, you need to register it differently. What you need to do is use the Resource.builder(ResourceClass)
method to get a builder where we can change the path. For example you can do something like this.
Resource swaggerResource = Resource.builder(ApiListingResource.class)
.path("foobar/swagger.{type:json|yaml}")
.build();
Then instead of the the ResourceConfig#register()
method, you use the ResourceConfig#registerResource(Resource)
method.
this.registerResource(swaggerResource);
Here's a complete test using Jersey Test Framework
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);
}
}
Thanks for the detailed explanation, though I have decided to leverage the holon platform to manage Jersey and Swagger
– Dark Star1
Nov 25 '18 at 13:20
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376448%2fis-it-possible-to-serve-the-swagger-root-from-a-sub-path-as-opposed-to-the-applc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Look at your link and the code
this.register(ApiListingResource.class);
That ApiListingResource
is the actual resource class that serves up the swagger.json
endpoint. If you look at the link, you can see the class is annotated with the path (the {type:json|yaml}
determines what type if data you will get back).
@Path("/swagger.{type:json|yaml}")
If you want to change the path, you need to register it differently. What you need to do is use the Resource.builder(ResourceClass)
method to get a builder where we can change the path. For example you can do something like this.
Resource swaggerResource = Resource.builder(ApiListingResource.class)
.path("foobar/swagger.{type:json|yaml}")
.build();
Then instead of the the ResourceConfig#register()
method, you use the ResourceConfig#registerResource(Resource)
method.
this.registerResource(swaggerResource);
Here's a complete test using Jersey Test Framework
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);
}
}
Thanks for the detailed explanation, though I have decided to leverage the holon platform to manage Jersey and Swagger
– Dark Star1
Nov 25 '18 at 13:20
add a comment |
Look at your link and the code
this.register(ApiListingResource.class);
That ApiListingResource
is the actual resource class that serves up the swagger.json
endpoint. If you look at the link, you can see the class is annotated with the path (the {type:json|yaml}
determines what type if data you will get back).
@Path("/swagger.{type:json|yaml}")
If you want to change the path, you need to register it differently. What you need to do is use the Resource.builder(ResourceClass)
method to get a builder where we can change the path. For example you can do something like this.
Resource swaggerResource = Resource.builder(ApiListingResource.class)
.path("foobar/swagger.{type:json|yaml}")
.build();
Then instead of the the ResourceConfig#register()
method, you use the ResourceConfig#registerResource(Resource)
method.
this.registerResource(swaggerResource);
Here's a complete test using Jersey Test Framework
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);
}
}
Thanks for the detailed explanation, though I have decided to leverage the holon platform to manage Jersey and Swagger
– Dark Star1
Nov 25 '18 at 13:20
add a comment |
Look at your link and the code
this.register(ApiListingResource.class);
That ApiListingResource
is the actual resource class that serves up the swagger.json
endpoint. If you look at the link, you can see the class is annotated with the path (the {type:json|yaml}
determines what type if data you will get back).
@Path("/swagger.{type:json|yaml}")
If you want to change the path, you need to register it differently. What you need to do is use the Resource.builder(ResourceClass)
method to get a builder where we can change the path. For example you can do something like this.
Resource swaggerResource = Resource.builder(ApiListingResource.class)
.path("foobar/swagger.{type:json|yaml}")
.build();
Then instead of the the ResourceConfig#register()
method, you use the ResourceConfig#registerResource(Resource)
method.
this.registerResource(swaggerResource);
Here's a complete test using Jersey Test Framework
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);
}
}
Look at your link and the code
this.register(ApiListingResource.class);
That ApiListingResource
is the actual resource class that serves up the swagger.json
endpoint. If you look at the link, you can see the class is annotated with the path (the {type:json|yaml}
determines what type if data you will get back).
@Path("/swagger.{type:json|yaml}")
If you want to change the path, you need to register it differently. What you need to do is use the Resource.builder(ResourceClass)
method to get a builder where we can change the path. For example you can do something like this.
Resource swaggerResource = Resource.builder(ApiListingResource.class)
.path("foobar/swagger.{type:json|yaml}")
.build();
Then instead of the the ResourceConfig#register()
method, you use the ResourceConfig#registerResource(Resource)
method.
this.registerResource(swaggerResource);
Here's a complete test using Jersey Test Framework
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);
}
}
answered Nov 19 '18 at 19:19


Paul Samsotha
149k19283467
149k19283467
Thanks for the detailed explanation, though I have decided to leverage the holon platform to manage Jersey and Swagger
– Dark Star1
Nov 25 '18 at 13:20
add a comment |
Thanks for the detailed explanation, though I have decided to leverage the holon platform to manage Jersey and Swagger
– Dark Star1
Nov 25 '18 at 13:20
Thanks for the detailed explanation, though I have decided to leverage the holon platform to manage Jersey and Swagger
– Dark Star1
Nov 25 '18 at 13:20
Thanks for the detailed explanation, though I have decided to leverage the holon platform to manage Jersey and Swagger
– Dark Star1
Nov 25 '18 at 13:20
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376448%2fis-it-possible-to-serve-the-swagger-root-from-a-sub-path-as-opposed-to-the-applc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown