Is it possible to serve the swagger root from a sub path as opposed to the applcation context root?












1














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-pathso it seems the basePath is hard-wired.










share|improve this question



























    1














    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-pathso it seems the basePath is hard-wired.










    share|improve this question

























      1












      1








      1







      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-pathso it seems the basePath is hard-wired.










      share|improve this question













      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-pathso it seems the basePath is hard-wired.







      spring-boot jersey-2.0 swagger-2.0






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '18 at 14:10









      Dark Star1

      2,902105595




      2,902105595
























          1 Answer
          1






          active

          oldest

          votes


















          1














          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);
          }
          }





          share|improve this answer





















          • 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











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          1














          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);
          }
          }





          share|improve this answer





















          • 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
















          1














          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);
          }
          }





          share|improve this answer





















          • 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














          1












          1








          1






          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);
          }
          }





          share|improve this answer












          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);
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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


















          • 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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

          Npm cannot find a required file even through it is in the searched directory