Using Spring Boot 2.3.1.
Here is a snippet from pom:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger-version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger-version}</version>
</dependency>
Where swagger version is last for now: 3.0.0.
Swagger configuration:
#Configuration
#EnableSwagger2
public class SwaggerConfiguration {
#Bean
public Docket swaggerApiDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.paths(PathSelectors.any())
.apis(RequestHandlerSelectors.basePackage("com.demo.controller"))
.build()
.apiInfo(apiDetails());
}
private ApiInfo apiDetails() {
return new ApiInfo("Carpark Controller API",
"Carpark Service for managing car parks",
"0.0.1",
"",
new springfox.documentation.service.Contact("Jan",
"www.demo.com",
""),
"API License",
"",
Collections.emptyList());
}
}
No Security configuration is added. No any server-path or some additional configuration.
When the application is up swagger JSON documentation is available:
http://localhost:8080/v2/api-docs
However, if to navigate to swagger UI:
http://localhost:8080/swagger-ui.html
The result will be:
There was an unexpected error (type=Not Found, status=404).
Tried to downgrade swagger version to 2.9.2 result is the same.
How to solve this issue?
Found solution for Spring Boot 2:
Get read from all other swagger dependencies from pom file. Only add next one:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
Swagger configuration could be the same as it was before with #EnableSwagger2.
Start the application.
Swagger UI page is different now:
http://localhost:8080/swagger-ui/index.html
Finally, it works!
Looking for a solution at the web for this issue found the following comment.
Saw the latest samples for Spring Boot version: BootWebmvcApplication
And build.gradle configuration for it.
Here is a link to other projects sources.
In POM.XML file you missed one dependency that has swagger starter
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
</dependency>
Related
To mitigate Log4j vulnerability(2.14 to 2.17), we have migrated from spring boot 2.4.0 to 2.6.4. Since Spring Fox was having some issues we migrated to spring doc.
Swagger UI works fine on local, when moved to server (JBOSS), we get white label error , sometimes it works .
can someone help me on this or best alternative to mitigate the Log4j vulnerability.
Used Below Dependency in pom.xml:
Spring Boot version from 2.4.0 to 2.6.4
<dependency>
<groupId>org.springdoc</groupId>
<artifact>springdoc-openapi-ui</artifact>
<version>1.6.6</version>
</dependency>
application.properties :
application-description=#project.description#
application-version=#project.version#
main class :
#Bean
public OpenAPI Internal_APIs(#Value("${application-description}") String appDesciption, #Value("${application-version}") String appVersion)
{
return new OpenAPI()
.info(new Info()
.title("Internal")
.version(appVersion)
.description(appDesciption)
.license(new License()
.name("Apache 2.0")
.url("http://springdoc.org")))
.externalDocs(new External Documentation()
.url("http://springdoc.org"));
}
I have a Spring Boot (2.2.6) project that has another starter as a dependency:
<dependency>
<groupId>com.companyX.teamY</groupId>
<artifactId>Z-starter</artifactId>
<version>0.0.1</version>
</dependency>
But I get a error java.lang.IllegalStateException: Failed to introspect Class because of missing
java.lang.ClassNotFoundException: springfox.documentation.spring.web.plugins.Docket
I don't find any springfox under project's external libraries dropdown (in IntelliJ) so I digged into the source of custom starter and found this:
#Bean
#ConditionalOnMissingBean(Docket.class)
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
.title("A Sample Project")
.version("0.0.1")
.build())
.select()
.apis(basePackage("com.companyX"))
.build();
}
But the starter project does have
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
in its pom.xml. If I add above to my Spring Boot project the error goes away so I am not sure why IntelliJ or maven isn't able to resolve such dependency: Spring Boot APP -> Custom Starter -/> SpringFox. What am I missing here?
Versions:
Java SDK: 1.8.0_231
Maven: 3.6.2
SpringBoot: 2.2.6.RELEASE for both projects
I ran mvn clean install and did not see any errors. The error only appears for one of those contextloads test method.
EDIT:
After some digging, this looks to be an issue with transitive dependencies not being resolved, but then I followed the steps of running mvn install on both projects to no avail.
I have a Spring-Boot 2.1.4 application with n-RestDoc tests for Flux controller.
Environment:
Spring-Boot 2.1.4
Junit 5.4.0
spring-restdocs-webtestclient
spring-webflux
If I add the spring-clout-starter-sleuth dependendcy to the app, some of the doc test fails in maven build.
Important on different environment different test classes fails with:
java.lang.IllegalStateException: org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext#6516dd09 has been closed already ....
If run the failling test with maven -Dtest=OptDocTest than the test will not fail, also if a set (not all) test where specified.
Dependendcy
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
<version>2.1.1.RELEASE</version>
<exclusions> <!-- exclude old spring versions -->
<exclusion>
<artifactId>*</artifactId>
<groupId> org.springframework.security</groupId>
</exclusion>
<exclusion>
<artifactId>spring-aop</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
All test looks simular
#ExtendWith({ RestDocumentationExtension.class, SpringExtension.class })
#AutoConfigureRestDocs("target/generated-snippets")
#SpringBootTest(//webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = { ArchimedesApplication.class })
class OptControllerDocTest {
#MockBean
private SrkConnector srkTConnector;
#Autowired
private ApplicationContext context;
private WebTestClient webTestClient;
#BeforeEach
void beforeEach(RestDocumentationContextProvider restDocumentation) {
this.webTestClient = WebTestClient.bindToApplicationContext(context)
.configureClient()
.filter(documentationConfiguration(restDocumentation))
.build();
}
#Test
void documentationTest() throws IOException {
this.webTestClient.post()
.uri("/opt")
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromObject(testRequest))
.exchange()
.expectStatus() // here the error occur
.isOk()
.expectBody() ...
}
All IT Test with a running Boot Application works correct.
I have no idea what goes wrong and why the AnnotationConfigReactiveWebServerApplicationContext is closed.
On a windows box the rest doc test for controller with a flux content fails on a linux box for controller with mono content.
It is solved if I fall back to spring-cloud-starter-sleuth in version 2.1.0.RELEASE and remove all exclusions.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
If I use version 2.1.1.RELEASE the error occur.
This has been raised as a bug in the sleuth github issue list github.com/spring-cloud/spring-cloud-sleuth/issues/1450
If you, as me, have had this situation in regard to junit tests, then I solved it by removing all annotations #DirtiesContext in every junit test class I had in my project.
Same issue I also met. I solved the issue by downgrade Sleuth to 2.0.3.RELEASE(2.1.0 also not work with me). Mine Spring Boot version is 2.0.5.RELEASE.
If 2.0.3 still not work for you, try more downgrade version from mvn repo
I have a running Springboot application which serves the URL http://localhost:8081/topics and returns me JSON response as expected.
I added actuator dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<scope>test</scope>
</dependency>
as suggested in tutorial
But when I hit http://localhost:8081/health it does not give expected result. It says
{
"timestamp": 1497952368055,
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/health"
}
Spring boot version is 1.5.4.RELEASE. And Java 1.8
What additional settings do I need to do ?
In Spring Boot 2.0.0 you have to use /actuator/health
and in the application.properties file add the following line:
management.endpoint.health.show-details=always
Do the following steps : -
Change the scope of actuator dependency from test to compile
Instead of using /health use /actuator/health
If you want to see details of the health status then addmanagement.endpoint.health.show-details=always in the application.properties file.
In your dependency you have declared
<scope>test</scope>
It means that
test
This scope indicates that the dependency is not required for normal
use of the application, and is only available for the test compilation
and execution phases.
If you want it available for normal use of the application remove <scope>test</scope> or change it to <scope>compile</scope>
Add Maven dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Application.properties
management.endpoints.web.exposure.include= "*" include all endpoints on actuator
or
management.endpoints.web.exposure.include= health if need only health endpoint
Maven dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Application.properties
spring.profiles.active=local
server.port = 9292
management.endpoints.web.exposure.include=env,health,metrics
For reference use below link:(step by step explanation)
https://www.youtube.com/watch?v=0Dj2tsK2V2g
I am trying to get Swagger UI working with Spring Boot 1.2.1. I followed the instructions at https://github.com/martypitt/swagger-springmvc and I added #EnableSwagger on my spring config.
I currently get back JSON when I go to http://localhost:8080/api-docs but no nice HTML.
I am using Maven and added the dependency on swagger-ui:
<dependency>
<groupId>org.ajar</groupId>
<artifactId>swagger-spring-mvc-ui</artifactId>
<version>0.4</version>
</dependency>
This is my complete list of dependencies:
<dependencies>
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>0.9.4</version>
</dependency>
<dependency>
<groupId>org.ajar</groupId>
<artifactId>swagger-spring-mvc-ui</artifactId>
<version>0.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
I also tried http://localhost:8080/docs/index.html as URL, but that just gives the "Whitelabel Error Page"
Update:
I created a test project on Github to show the problem: https://github.com/wimdeblauwe/springboot-swagger-test
Your problem lies in your SwaggerConfiguration file. You need to take out #EnableWebMvc, because this causes the default Spring Boot view resolver to be overwritten by the default 'SpringWebMvc' one which serves static content differently.
By default, Spring Boot will serve static content from any of the following directories:
/META-INF/resources/
/resources/
/static/
/public/
including webjars.
I had the same problem and I found this in the documentation: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-auto-configuration
If you want to take complete control of Spring MVC, you can add your own #Configuration annotated with #EnableWebMvc. If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) you can add your own #Bean of type WebMvcConfigurerAdapter, but without #EnableWebMvc.
I hope this helps.
I have swagger-ui v0.4 (with spring v4.14 & swagger-springmvc v0.9.4) working fine, although I had some similar problems at first. It seems like this class does the trick.
#Configuration
#EnableSwagger
public class SwaggerConfig extends WebMvcConfigurerAdapter {
#Autowired
private SpringSwaggerConfig springSwaggerConfig;
#Bean
public SwaggerSpringMvcPlugin customImplementation() {
return new SwaggerSpringMvcPlugin(springSwaggerConfig).apiInfo(
apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfo(/* strings */);
}
#Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
I believe the relevant thing is the overridden configureDefaultServletHandling. And on my main WebApplicationInitializer, I have:
#Import(SwaggerConfig.class)
Finally, I fixed the issue with the UI's location box showing "http://localhost:8080${pageContext.request.contextPath}/api-docs" by including this in my dependencies:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<!--<version>8.0.15</version>-->
<scope>provided</scope>
</dependency>
That provides something related to JSP processing. It is included in the dependencies of spring-boot, but it isn't normally provided.
Hope that helps.
If you are experiencing this issue with version springfox swagger-ui 3.x or greater..
try the below url.. It worked for me..
http://localhost:8080/swagger-ui/
For complete swagger documentations steps, refer:
http://muralitechblog.com/swagger-rest-api-dcoumentation-for-spring-boot/
I have the same issue but this link works for me: http://localhost:8080/sdoc.jsp
It pre-populates the swagger ui resource url box with :
http://localhost:8080${pageContext.request.contextPath}/api-docs
and when I hand edit it removing ${pageContext.request.contextPath} and hit Explore it shows me my api doc and I can even try my endpoints successfully. So definitely an issue but probably not picking up ${pageContext.request/contextPath}.
Looking at the source the javascript has:
url: window.location.origin + "${pageContext.request.contextPath}/api-docs"
on a static swagger ui html I have this piece is coded as:
discoveryUrl:"./resource-list.json"
I hope this is a bit helpful
As indicated by Tamas above, the problem lies in using #EnableWebMvc, which goes around the default setup and skips some things Swagger needs. Switching that to #EnableSwagger2 for me was enough to fix the issues in my project, which showed similar symptoms.
I have faced same issues in my project. resolved issue with below steps.
Added below dependencies in pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
Added swagger2 ui configuration as below in the project level
#Configuration
#EnableSwagger2
#EnableAutoConfiguration
public class SpringFoxConfig {
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
Then able to get the swagger ui documentation http://localhost:8080/swagger-ui/
swagger api docs http://localhost:8080/v2/api-docs
I have done separate config for Swagger and my problem was that without #EnableAutoConfiguration it was not working properly.
#Configuration
#EnableSwagger2
#EnableAutoConfiguration
public class SwaggerConfig {
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
I would suggest you to use #EnableSwagger2 tag and follow the steps and code from here: https://github.com/sanketsw/SpringBoot_REST_API
Also i am using following dependency which works perfectly fine:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
<scope>compile</scope>
</dependency>