404 error on Swagger UI with Spring (springdoc-openapi configuration) - spring

I am adding swagger UI to my Spring boot application. When I try to access the swagger-ui.html. I get the 404 error.
Config class :
#Configuration
public class SwaggerConfig {
#Bean
public OpenAPI springShopOpenAPI() {
return new OpenAPI()
.info(new Info().title("JOYAS-STOCK API Docs")
.description("JOYAS-STOCK REST API documentation")
.version("v1.0.0"));
}
}
appliaction.properties :
#swagger-ui config
springdoc.swagger-ui.path=/swagger-ui
springdoc.swagger-ui.operationsSorter=method
springdoc.swagger-ui.tagsSorter=alpha
pom.xml :
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.13</version>
</dependency>
error message :
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
There was an unexpected error (type=Not Found, status=404).
i started with the implementation of the configuration of swagger
and apparently it's not working.
click to see screen of the error

Resolved.
the issue was in the versions, they were not compatible! i was using springdoc-openapi v1 with spring boot 3.
which is wrong! with spring boot 3, springdoc-openapi v2 should be used.
see documentation : https://springdoc.org/v2/

Related

Refresh not working when using spring cloud starter and fabric8

I think that there is an issue when using these two dependencies in the same projet. I'm talking about spring-cloud-starter and spring-cloud-starter-kubernetes-fabric8-config.
I'm using
Spring Boot : 2.7.7
Spring Cloud : 2021.0.5
What i'm trying to build is a spring boot application on kubernetes. Each time a configmap or a sercret get changed, the app context should be updated with the new values. To achieve this, i'm using the spring cloud watcher.
https://docs.spring.io/spring-cloud-kubernetes/docs/current/reference/html/#spring-cloud-kubernetes-configuration-watcher
For the configmap part, everything works fine but when i tried to use a secret i noticed a strange behavior. The spring watcher calls my pods on the /actuator/refresh endpoint but nothing get updated. Actually, even on my local machine, the /refresh endpoint returns 200_OK but nothing is updated.
Here's the code snippet :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-fabric8-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
#Configuration
#ConfigurationProperties(prefix = "metadata")
#PropertySource(value = "classpath:acl-default.yaml", factory = YamlPropertySourceFactory.class)
#PropertySource(value = "file:${user.home}/acls/acl.yaml", factory = YamlPropertySourceFactory.class, ignoreResourceNotFound = true)
#Slf4j
#Getter
#Setter
public class ACLConfig implements InitializingBean {
private List<User> users;
The ACLConfig class will load our users credentials let's say based on a file from the files system under ${user.home}/acls/acl.yaml otherwise it will get loaded from the classpath.
The content of ${user.home}/acls/acl.yaml is :
metadata:
users:
- name: Richard
password: kjqsd78jkdq-local
- name: Richard
password: jqsd98ds78-local
- name: Richard
password: kSkjqf887qsd-local
bootstrap.properties
server.port=8080
spring.application.name=demo
spring.cloud.kubernetes.config.sources[0].name=${spring.application.name}
spring.cloud.kubernetes.config.sources[0].namespace=spring-cloud-watcher
application.properties
logging.level.root=INFO
logging.level.com.example.demo=DEBUG
management.endpoints.web.exposure.include=info,health,refresh
spring.config.import=optional:file:${user.home}/acls/acl.yaml
So as i said, this example does not work (the acl file update will not trigger the spring context refresh).
To make it work, you have to :
delete the spring-cloud-starter-kubernetes-fabric8-config dependency from pom.xml,
change the content of ${user.home}/acls/acl.yaml,
call the /actuator/refresh,
check the logs : you'll see that ACLConfig -> users List will be updated
You can find the example on my repo : https://github.com/mamdouni/spring-watcher-example
Delete the fabric8 dependency is not an option for me because i need to run this on kubernetes but i don't understand why it blocks the context refresh !!!
Any help will be appreciated.

Swagger UI - org.springdoc - throws whitelabel error

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"));
}

OpenAPI 3 Casting Exception fails to show Api-Docs on localhost

I am converting our current Swagger configuration from spring fox (Swagger2) to OpenAPI 3.0 (Swagger3), however when I run the SpringBoot project locally and attempt to hit localhost:8080/v3/api-docs I get a casting exception:
{"errors":[{""errorCode":"INTERNAL_SERVER_ERROR", "errorDescription":"Internal server error. Unable to process your request", "stackTrace": "java.lang.ClassCastException: java.lang.String cannot be cast to com.package.ObjectInQuestion}]}
Swagger2 used to work fine but once I introduced the OpenAPI dependencies below, and take out all Swagger2 references, it breaks.
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-open-ui</artifactId>
<version>1.5.5</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-open-webmvc-core</artifactId>
<version>1.5.5</version>
</dependency>
I even added the webmvc-core alongside to see if that fixed the issue but to no avail. Updated all the tags to the appropriate OpenAPI tags to our API's. Tried with Docket(Swagger2) and OpenAPI(Swagger3) #Beans.
The ObjectInQuestion is not even used in the project, its included as a class model in our common jars so I suspect the issue is with the Swagger3 configuration.
I used the following link to configure OpenAPI, springdoc.org/#migrating-from-springfox

Swagger UI page is found for Spring Boot 2

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>

SpringBoot cannot provide jsp files in webapp Folder

I am new in springboot trying to simple webapp.
Using springboot 2.1.0 application.properties file like below.
I have jsp files in src/main/webapp/WEB-INF/jsp
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
My controller is .
#Controller
class WelcomeController {
#GetMapping("/")
fun showWelcomePage(model: Model): String {
model["name"] = "asdas"
return "welcome"
}
}
When I put jsp files in /main/resources/META-INF/resources/WEB-INF/jsp it works otherwise got below error
There was an unexpected error (type=Not Found, status=404).
/WEB-INF/jsp/welcome.jsp
Also pom has tomcat-embed-jasper dependency
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
Any idea?
Ensure that jasper dependency and embedded tomcat dependency versions should be same otherwise it will be an issue.
Also try putting a jsp page outside itself like src/main/webapp/abc.jsp and remove view resolver and return directly like "abc.jsp" and see if that works.
see this at 7:30
are you using intellij? i had experience about same spring boot (jsp view) project is working in eclipse, but it's not work in intellij. i requested to jetbrain about this issue, they are say to me that "it's right, but we recomment use template engine like handlebars"

Resources