WhiteLabel error page while trying to access swagger-ui.html using springfox configuration - spring-boot

I am trying to generate Swagger documentation from a springboot project using Springfox and following https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api documentation.
Initially I got the error "Full authorization is required to access the resource" since I am using OAuth2 in my application. I changed the configuration to permit all the requests ending with /swagger-ui.html.
Now I have been getting "WhiteLabel error page - This application has no explicit mapping for /error" while trying to access /swagger-ui.html on my local.
I went through various posts but none of the solutions worked for me - I am not using #webmvcconfiguration which can interfere.
Can anyone help?

For Swagger 3.0, The URL is changed to
http://localhost:8080/swagger-ui/index.html

Remove the v2 dependencies from your pom.xml or comment them out.
<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>
Add the springfox-boot-starter
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
Change the URL in your browser to:
http://localhost:8080/swagger-ui/index.html
The general form for the URL is:
http://host/context-path/swagger-ui/index.html
OR
http://host/context-path/swagger-ui/
For more info check this link that leads to the relevant documentation:
https://springfox.github.io/springfox/docs/snapshot/#changes-in-swagger-ui

This is how I solved my problem. Here is my detailed code, if someone want to look.
https://github.com/xbox2204/SpringBoot-JPA-Swagger
Now, I used 3.0.0-SNAPSHOT and a latest spring-boot starter project I created from here:
https://start.spring.io/
My pom.xml, I added following dependencies:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.springframework.plugin</groupId>
<artifactId>spring-plugin-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.plugin</groupId>
<artifactId>spring-plugin-core</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
In my application.properties, I added following:
spring.resources.add-mappings=true
In my SpringBoot Main/Runner class, I added these annotations
#EnableWebMvc
#EnableSwagger2
#SpringBootApplication
My Docket returning function looked like this
#Bean
public Docket productApi() {
Contact contact =new Contact(
"Vineet Mishra",
"https://github.com/xbox2204",
"whatwillyoudo#withmyemail.com"
);
ApiInfo apiInfo= new ApiInfoBuilder().title("VINEET SPRING-BOOT API")
.description("Spring-Boot for all")
.termsOfServiceUrl("jUST CHILL!!!")
.contact(contact)
.licenseUrl("something#something.com").version("1.0").build();
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo)
.select()
.apis(RequestHandlerSelectors.any())
.build();
}
Finally, I accessed my swagger-ui from
http://localhost:8080/swagger-ui/index.html#
Image of final result

The swagger-ui.html page makes a number of calls to get all the details. If you use your browser's dev tools (usually just press F12 to open) you will see failing requests in the network tab. You'll need to permit requests to
"/v2/api-docs",
"/swagger-resources/**",
"/swagger-ui.html**",
"/webjars/**"
There's some info in the springfox docs , do a find for 'security' or 'authorization'

Related

Getting "Finished Loading Resource Information. Rendering Swagger UI..." on loading swagger after migration from spring to sprinboot

We just migrated our application from Spring to Sprinboot (2.7.0). whenever i load swagger, all the apis dont get loaded and i get this line "Finished Loading Resource Information. Rendering Swagger UI..." on the top.
But http://host:port/v2/api-docs loads all the apis.
earlier we have below swagger related dependencies in our 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>${version.io.springfox}</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.6.8</version>
</dependency>
dependencies. I tried with that, i got same issue.
Later i tried adding springfox-boot-starter dependency and commenting out specific swagger based spring dependencies but no luck. I tried to search for a solution but couldnt find anything concrete.
Any idea why this is happening?

RESOLVED - Spring Boot Starter Mail - failed to access class com.sun.activation.registries.LogSupport from class javax.activation.MimetypesFileTypeMap

I want to share the solution for above issue when using Spring Boot Starter Mail and attempt to create instance of MimeMessageHelper results in exception:
failed to access class com.sun.activation.registries.LogSupport from class javax.activation.MimetypesFileTypeMap
The issue is behind the jaxb-core dependency in version 4.0.0 which brings angus-activation library dependency. The library if loaded first doesn't have a public class of LogSupport.
The correct source of LogSupport is from com.sun.activation:jakarta.activation library.
Solution is to exclude the following in jaxb-core dependency:
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>4.0.0</version>
<exclusions>
<exclusion>
<groupId>org.eclipse.angus</groupId>
<artifactId>angus-activation</artifactId>
</exclusion>
</exclusions>
</dependency>
At the same time the following dependency should be available:
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>jakarta.activation</artifactId>
<version>2.0.1</version>
</dependency>

Spring Boot No Class found error though starter dependency has added classes in classpath

Despite having appropriate jars in classpath Spring Boot throws - java.lang.ClassNotFoundException: org.apache.camel.spring.spi.XmlCamelContextConfigurer
Any suggestions what is missing here.
I have added the respective starters in pom.xml as shown below:
<dependency>
<groupId>org.springframework.boot</groupId>[![enter image description here][1]][1]
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-cxf</artifactId>
<version>2.19.3</version>
</dependency>
The project maven dependencies also shows the class availability, as in given image attachment.
the answer is here - https://stackoverflow.com/a/34271507/2885422 It all depends how maven loads the class. There are 2 version of camel-spring.jars available in classpath (2.20.1 and 2.19.3. Ref: my original post image)
And org.apache.camel.spring.spi.XmlCamelContextConfigurer class is available only in 2.20.1 jars. And maven by default looks in earlier one and once package found matching but no class found throws error (?)
And the reason I believe 2.19.3 get loaded is Apache-cxf jars is of 2.19.3. Unfortunately our project repository does not have apache-cxf starter jars.
https://stackoverflow.com/a/34271507/2885422
Solution: - I hope it may be helpful for future references
By adding options I could resolve the issue,by having exclusion clause added as given below. Thus I could load only required version jars.
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-cxf</artifactId>
<version>2.19.3</version>
<exclusions>
<exclusion>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
</exclusion>
</exclusions>
</dependency>'

Spring Boot logging with Log4j2

Written simple POC to prove and test Spring Boot and log4j2 compatibility. Once successful I will move it to real application.
Please refer my POC source code:
https://github.com/Dennyss/SpringBootLog4j2POC
I know/read about Spring version and log4j2 compatibility from:
How to set up Spring Boot and log4j2 properly?
Found and tried recommendations described here:
Spring-Boot logging with log4j2?
But still not working. The problem is that both application logs and Spring logs are printing to console only.
Please refer maven dependencies below (from POC):
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.6.2</version>
</dependency>
</dependencies>
If I don't exclude Spring's logback and don't add boot-starter-log4j2 then application logs are printing to application file but Spring logs are not printing at all. I feel the problem somewhere with dependencies. Appreciate any help.
According to the Spring Boot Logging documentation, the location of the logging configuration file can be specified using the logging.config property. I noticed that your start.sh script is passing -Dlog4j.configurationFile. Normally, this would be sufficient for direct Log4J 2 integration, but Spring Boot uses logging.config.
After switching to this, it writes to the log files:
java -Dlogging.config=/share/LogPOC/log4j2.xml -cp poc.jar:libs/* com.poc.logger.Application

Changing Name and Description endpoint in Swagger-ui

I am using Spring boot and Spring Data Rest to implement my Rest API. To document it, I have been using Swagger, with these maven dependencies:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-data-rest</artifactId>
<version>2.6.1</version>
</dependency>
I would like to change and customize a bit my Swagger UI page. I want to change the name and the description it appears in the html page, concretely these names highlighted in red in the screenshot, but nothing works.
I've been trying adding the #Api annotation to my entities and repositories classes/interfaces, but nothing works.
Any idea about how to customize it?
Thanks!
Use the #Api annotation with tags and description.
#Api(description="Device APIs", tags = "Device")
Please note that description is deprecated but it works.

Resources