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:
This is with the REST Endpoint expanded (as you can see there's no text field to type the id inside):
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):
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):
Is this a possible bug (the non-interactive text field) in Spring Fox 2.9.2?
Why is the swagger-ui looked dumbed down (the colors being different & not being so sharp looking, etc)?