1

I am using tynamo resteasy with my Tapestry 5.4 project. I'd like to incorporate Swagger to document and share API with other other teams. While I see that swagger is already in RestEasy's project dependency, it does not work "out of the box"

I've added

@Api to the "Resource" class (located in /rest/ package) and @ApiOperation to the GET method

Do I need to change AppModule or web.xml in anyway to hook the swagger UI? Is there an example (github etc.) anywhere that I can follow?

Also, will the swagger api be available at localhost/swagger/myendpoint?

labheshr
  • 2,350
  • 2
  • 19
  • 28

3 Answers3

2

Take a look at this old commit: https://github.com/tynamo/tapestry-resteasy/commit/9a4c2979fda83900480449f25df8cb5e919e4306

Pay special attention to the SwaggerModule. The code is rather old and I'm almost sure that is not going to work out of the box if you copy&paste it as is, but it will give you a very good idea of how the connections between the projects work and the setup required.

ascandroli
  • 3,239
  • 1
  • 12
  • 15
  • i use tynamo resteasy already. Isn't this code already integrated into the jar I get from gradle/maven? – labheshr May 31 '17 at 12:59
  • No, for some reason (I don't remember why) I left it on the "test" folder so the SwaggerModule is not part of the final .jar release that you get from the central repositories. – ascandroli May 31 '17 at 15:02
  • thanks a lot..I used parts of your technique but have combined it with swagger.io lib instead of wordnik's swagger. I also posted the full solution as an answer. – labheshr Jun 05 '17 at 00:20
  • on a side note: would you have any insight on this: https://stackoverflow.com/questions/44360181/tapestry-5-4-how-to-use-an-external-dist-package ? thanks a ton! – labheshr Jun 05 '17 at 00:28
2

Here's a step by step process:

Get swagger-jaxrs: https://mvnrepository.com/artifact/io.swagger/swagger-jaxrs/1.5.0

Create a SwaggerModule.java in "modules" directory (example: com.mysite.mypkg.modules)

public class SwaggerModule {
  @Contribute(SymbolProvider.class)
  @ApplicationDefaults
  public static void provideSymbols(MappedConfiguration<String, Object> configuration) {
    configuration.add(ResteasySymbols.CORS_ENABLED, true);
  }

  @Contribute(javax.ws.rs.core.Application.class)
  public static void contributeApplication(Configuration<Object> singletons) {
    singletons.addInstance(io.swagger.jaxrs.listing.ApiListingResource.class);
    singletons.addInstance(io.swagger.jaxrs.listing.SwaggerSerializers.class);
  }

  @Startup
  public static void swagger(javax.ws.rs.core.Application application,
      BaseURLSource baseURLSource,
      @Symbol(InternalConstants.TAPESTRY_APP_PACKAGE_PARAM) String basePackage,
      @Symbol(ResteasySymbols.MAPPING_PREFIX) String restPath,
      @Symbol(SymbolConstants.APPLICATION_VERSION) String version) {
    application.getSingletons(); 
    BeanConfig config = new BeanConfig();
    config.setSchemes(new String[]{"http"});
    config.setVersion("1.0.2");
    config.setHost("localhost:8080");
    config.setBasePath("/mysite" + restPath);
    config.setTitle("Mysite Rest Documentation");
    config.setResourcePackage("com.mysite.mypkg.rest");//where your rest resources are located
    config.setScan(true);
  }

On your AppModule.java, import the SwaggerModule (Tapestry 5.4)

@ImportModule(SwaggerModule.class)
public class AppModule {...
}

The swagger.json and swagger.yaml can now be accessed as:

http://localhost:8080/mysite/rest/swagger.json

Many thanks to @ascandroli above for pointing out the basics

labheshr
  • 2,350
  • 2
  • 19
  • 28
1

Looks like I'm only three years late to the party, but anyway I needed swagger integration for a project, so I took @ascandroli's code, updated it to OpenAPI 3.0 (swagger core 2.x) and made swagger integration its own, separate submodule to make it work out-of-the-box for users, by just adding a dependency. The details are bit sketchy, but the tapestry-resteasy-swagger module source is at github and the GAV coordinates for now are org.tynamo:tapestry-resteasy-swagger:0.0.2. I'll add more info as time allows, but check the integrated test project in the meantime.

Kalle
  • 1,927
  • 1
  • 20
  • 19