0

I've been looking into swagger as a possible way to automatically generate documentation for my team's api http://swagger.io/getting-started/. It seems very promising, but I find their documentation lacking.

That being said I have a few very basic questions.

  1. Is it possible to use swagger with just a Spring application? Our application is neither a jersey or JAX-RS application. Does anyone know if a plain Spring application can be used with swagger? If so can a link or set of instructions be provided?

I'd found this link http://blog.zenika.com/index.php?post/2013/07/11/Documenting-a-REST-API-with-Swagger-and-Spring-MVC however they skim over the bits about setting the properties file.

  1. swagger cites many different tools on their site swagger core, codegen, ui, and swagger editor. Is only swagger-core needed to generate their basic API documentation or is a combination of swagger tools required?
OrwellHindenberg
  • 4,988
  • 8
  • 38
  • 58
  • I haven't used swagger, but the source is released so you can definitely mold it to function with spring... just not sure if anyone else has done this yet. – Ya Wang Oct 16 '15 at 20:00
  • The last time I recall trying, I believe Swagger took over the root path of any resource, which was a non-starter for us. That was two years ago, and I don't think I've looked at it since (I don't think it still does that anymore). – Makoto Oct 16 '15 at 20:10
  • 1
    Let me just point out the irony that the documentation is lacking on a product intended to generate (wait for it) documentation. On a non-snarky note, I'll comment that it would cause me to have `serious` doubts about the quality of the product. – CPerkins Oct 16 '15 at 20:18
  • You might be interested in the [new](http://spring.io/blog/2015/10/07/spring-rest-docs-1-0-0-release) [REST](http://docs.spring.io/spring-restdocs/docs/current/reference/html5/#getting-started) [Docs](http://docs.spring.io/spring-restdocs/docs/current/reference/html5/) – xenoterracide Oct 17 '15 at 02:02
  • @Makoto that was many years ago, take a look at [swagger 1.5.x](https://github.com/swagger-api/swagger-core) – fehguy Oct 19 '15 at 00:46
  • For latest(Jan 21) configuration with V2 and V3 check - https://stackoverflow.com/a/64333853/410439 – Ravi Parekh Feb 18 '21 at 15:07

3 Answers3

1

There is a page dedicate to support swagger frameworks.

What you are looking for is SpringFox.

Automated JSON API documentation for API's built with Spring

Basically you apply the configuration using a Docket object like this pointing to your API context. You'll have to take your time since there is a little bit of configuration for making it to work, not mentioning scaling it to your application needs.

 @Bean
    public Docket documentation() {
        return new Docket()
          .select()
            .apis(RequestHandlerSelectors.any())
            .paths(regex("/api/.*"))
            .build()
          .pathMapping("/")
          .apiInfo(metadata());
    }

As you might already know, you can add annotations such as possible HTTP response codes, etc, among many other capabilities. Very promising indeed.

@ApiOperation(value = "doStuff", nickname = "doStuff", response = DoStuffResult.class)
@Responses({
    @ApiResponse(code =  404, message ="Not found", response = GenericError.class),
    @ApiResponse(code =  400, message ="Invalid input", response = GenericError.class)
})
@RequestMapping(method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<?> doStuff(@RequestBody DoStuffCommand command) {
    // Stuff
}

Here is the best example I could find online, very short and objective.

Evandro Pomatti
  • 13,341
  • 16
  • 97
  • 165
1

For me following worked:

Added the following dependencies to the pom:

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.2.2</version>
    </dependency>

Then added @EnableSwagger2 to the spring configuration file and registered resource handler needed for the swagger UI like:

@Configuration
public class YourConfigFileHere extends WebMvcConfigurerAdapter {


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations(
            "/resources/");

        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
            "classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**").addResourceLocations(
            "classpath:/META-INF/resources/webjars/");
    }

    // rest of the configuration
}

This would get the basic swagger UI up and running. If you want to customize, you can do so by adding a swagger configuration file annotated with @EnableSwagger2 and import it into spring configuration file with @Import.

For local environment, you can access swagger-ui at:

http://localhost:8080/{context-root}/swagger-ui.html
cpd214
  • 605
  • 7
  • 11
  • Where should I be defining these registered resources? In the spring configuration file? Where should I look for the url that will be displaying the documentation? – OrwellHindenberg Oct 19 '15 at 21:00
0

As stated above, springfox is a first-class swagger library for spring-mvc. You can also use spring configuration as demonstrated in the swagger-samples repository.

You can see an example of this working by visiting http://editor.swagger.io and creating a swagger definition. From there, you can download a spring-mvc server with swagger support.

fehguy
  • 6,724
  • 25
  • 23
  • I've tried following the guides I find online and get into maven dependency hell. I spent hours getting maven dependencies to stop failing. The repo I have is jcenter-snapshots. The deps I have are springfox-swagger2 and springfox-swagger-ui. Even when maven stops complaining I still get errors like " Failed to retrieve JNDI naming context for container". Am I missing something maven-wise or should I just put swagger/springfox away? – OrwellHindenberg Nov 17 '15 at 19:39