-1

Using Spring Fox 2.9.2 with Spring Boot 2.1.5 RELEASE, I am not able to use the interactive UI generated by Swagger.

This is with the REST Endpoints not expanded:

enter image description here


This is with the REST Endpoint expanded (as you can see there's no text field to type the id inside):

enter image description here


Maven pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
    <relativePath/>
</parent>

<dependencies>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>
</dependencies>


Swagger2Config.java:

package com.myservice.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).select()
                                   .apis(RequestHandlerSelectors
                                   .basePackage("com.myservice"))
                                   .paths(PathSelectors.any())
                                   .build()
                                   .apiInfo(apiEndPointsInfo());
    }

    private ApiInfo apiEndPointsInfo() {
        return new ApiInfoBuilder().title("MyService API")
                                   .description("Buildings you measure.")
                                   .contact(new Contact(null, null, null))
                                   .license("Apache 2.0")
                                   .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                                   .version("1.0.0")
                                   .build();
    }
}

Also noticed is that I didn't need to use any Swagger specific annotations (e.g.@ApiOperation & @ApiResponse) inside the RestController (I did try putting them above getByUsingId(Integer id) method, and whereas my documentation was visible it still didn't have the id text field):

RestController:

@RequestMapping(value = {"/{id}" }, method = RequestMethod.GET, produces = "APPLICATION/JSON")
public ResponseEntity<Object> getByUsingId(@PathVariable(value = "id", required = true) Integer id) 
throws IOException {
       MyResponse response = myDao.getById(id);
       if (response == null) {
           return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
       }
       return new ResponseEntity<Object>(response, headers, HttpStatus.OK);
}

Comparing this to an old Spring Boot 1.5.6 RELEASE project, Spring Fox 2.6.1 has a much better UI and it is interactive (notice the better colors and the visible text field when expanded):

enter image description here

This is exactly what I need and want.


Maven pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
</parent>

<dependencies>
    <dependency>
       <groupId>io.springfox</groupId>
       <artifactId>springfox-swagger2</artifactId>
       <version>2.6.1</version>
       <scope>compile</scope>
    </dependency>

    <dependency>
       <groupId>io.springfox</groupId>
       <artifactId>springfox-swagger-ui</artifactId>
       <version>2.6.1</version>
       <scope>compile</scope>
    </dependency>
<dependencies>

Swagger2Config.java:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.base.Predicates;

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@Configuration
public class SwaggerConfig {

    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                .paths(Predicates.not(PathSelectors.regex("/error"))).build().apiInfo(metaInfo());
    }

    private ApiInfo metaInfo() {
        ApiInfo apiInfo = new ApiInfo("Hierarchy Project", "Hierarchy Demo as a Spring Boot based Microservice", "1.0", null,
                new Contact("", "", ""), "", "");

        return apiInfo;
    }
}

RestController:

@ApiOperation(httpMethod = "GET", value = "Get All Records Within a Level of Hierarchy Based On Parent Node.", 
                                  notes = "List records within a level of the hierarchy, for a given parent node.",
                                  produces = "application/json")
@ApiResponses(value = 
             { 
                @ApiResponse(code = 200, message = "Successful GET Command", response = String.class),
                @ApiResponse(code = 404, message = "Not Found") 
             }
)
@RequestMapping(value = { "/api/nodes" }, method = RequestMethod.GET, produces = "APPLICATION/JSON")
public ResponseEntity<Object> getHierarchyByUsingNode(Node node) throws Exception {
    if (null == node) {
        return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
    }
    List<Node> nodes = nodeService.getHierarchyPerParentNode(node);

    if (nodes.isEmpty()) {
        return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
    } 
    else {
        return new ResponseEntity<Object>(nodes, headers, HttpStatus.OK);
    }
}

When I tried using the Spring Fox 2.6.1 dependencies inside my Spring Boot 2.1.5.RELEASE project, the swagger-ui.html page didn't even render!

As one can notice that whereas Spring Fox 2.9.2 doesn't need annotations, generated swagger-ui.html page not only looks different (in terms of colors, etc) but it doesn't even have any text fields like my second example, Hierarchy Project, does.


Question(s):

  1. Is this a possible bug (the non-interactive text field) in Spring Fox 2.9.2?

  2. Why is the swagger-ui looked dumbed down (the colors being different & not being so sharp looking, etc)?

PacificNW_Lover
  • 4,746
  • 31
  • 90
  • 144

2 Answers2

1

Figured it out... The older versions of Swagger used the "Try it Out" button to execute the REST calls...

Now, it's a two-step process, you have to click on the "Try it Out" button first, enter in the id inside the text field, and then click on the very horizontally long blue "Execute" button.

This was a horrible change (in terms of aesthetics and UI design) and is not that intuitive as the previous design was.

PacificNW_Lover
  • 4,746
  • 31
  • 90
  • 144
  • Why does this answer have a negative rating? It's clear that the issue was that it went from a one-step process to a two-step process. Why is that considered an invalid / "bad" answer? – PacificNW_Lover Nov 10 '19 at 02:25
-2

Try to add this to your pom.xml

            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

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

and also

@Bean
    public Docket apiDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("myPackage"))
                .paths(PathSelectors.any())
                .build();
    }

where myPackage is the package that contains the Spring boot starter class.

Hope it will solve your problem. :)

  • I have those dependencies in my pom.xml - I put that in the top of my post, you didn't see it? – PacificNW_Lover Nov 09 '19 at 00:14
  • For me is working with that configuration that I wrote here, did you tried it? (try without ApiInfo), just try it. – dragos.pavel Nov 09 '19 at 00:21
  • I tried it without ```apiInfo()``` and it still didn't work... Its the same non-interactive page without text fields and the ugly font colors (e.g. black background for HTTP Response Error codes)... – PacificNW_Lover Nov 09 '19 at 00:29
  • Did you hitted the try it out button from top right corner? It’s the new interface of swagger because you are using a different version than the other one ( the second print screen from tour post)? – dragos.pavel Nov 09 '19 at 00:37
  • Yes, I clicked on the "Try it Out" button, but its useless because I can't enter the id value due to the lack of text field... So, the new UI interface has been dumbed down and has no working text fields? – PacificNW_Lover Nov 09 '19 at 00:43
  • Can you provide a print screen? – dragos.pavel Nov 09 '19 at 00:47