Swagger UI with Java9 (spring boot 2.0) - spring

I'm trying to make the swagger UI work with my service. I'm using spring boot 2.0 and Java 9. The following dependencies are added for swagger
compile('io.springfox:springfox-swagger2:2.8.0')
compile('io.springfox:springfox-swagger-ui:2.8.0')
The swagger is configured as it should
#Configuration
#EnableSwagger2
public class SwaggerConfig {
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
Compiles well but when I starting up the application with the BootRun command it fails due:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xmlModelPlugin': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [springfox.documentation.schema.XmlModelPlugin] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader#4f8e5cde]
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlType
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
... 25 common frames omitted
What could be the problem? Not compatible with Java9? Then can I make it work somehow?

Yes, that's incompatibility with java 9.
I think you have three options:
add explicit dependency to xml apis like compile('javax.xml.bind:jaxb-api:2.3.0')
update springfox version to 2.9.0
add --add-modules java.xml.bind to jvm startup params (this is more just a workaround instead of a fix, in comparison to other options, since this module will be removed in the later versions of java)
Further reading on Spring Boot and java 9: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-with-Java-9-and-above

I am not sure about this very specific error, but in general Swagger 2.9.x seems to have some incompatibility with SpringBoot 2.0.x
Some documented issues:
https://github.com/springfox/springfox/issues/1773 (main one)
https://github.com/springfox/springfox/issues/2501
There is a 3.0.0-SNAPSHOT version which might be more stable with this Spring Boot version.

version 2.9.0 could not be found in maven central. Available versions are 2.9.1 and 2.9.2
https://mvnrepository.com/artifact/io.springfox/springfox-swagger2
As suggested by #eis after upgrade from 2.8.0 to 2.9.2 fixed the issue for me

Related

ASM ClassReader failed to parse class file - Migration from 2.1.8.RELEASE to 2.7.7

After migration from 2.1.8.RELEASE to 2.7.7, I get this error: ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet. Unsupported class file major version 61
I checked java 17 is used.
The piece of code causing this error is :
public class ApplicationWebXml extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
/**
* set a default to use when no profile is configured.
*/
DefaultProfileUtil.addDefaultProfile(application.application());
return application.sources(MyApp.class);
}
}
This is probably due to an outdated Spring Framework version on your classpath. ASM's ClassReader is shaded in the spring-core module and the corresponding spring-core module with Spring Boot 2.7.x should support Java 17.
Running into this type of problems means that your application should probably apply the Spring Boot dependency management with Maven or Gradle.

Exception in thread "main" java.lang.StackOverflowError at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:242)

I am working on a spring multi module project. I have upgraded spring version from 3.x to 5.3.17. After upgradation, while running project Iam getting following error:
Exception in thread "main" java.lang.StackOverflowError
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:242)
at org.apache.commons.logging.LogAdapter$Slf4jAdapter.createLocationAwareLog(LogAdapter.java:130)
at org.apache.commons.logging.LogAdapter.createLog(LogAdapter.java:91)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:67)
at org.slf4j.impl.JCLLoggerFactory.getLogger(JCLLoggerFactory.java:88)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:242)
at org.apache.commons.logging.LogAdapter$Slf4jAdapter.createLocationAwareLog(LogAdapter.java:130)
at org.apache.commons.logging.LogAdapter.createLog(LogAdapter.java:91)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:67)
at org.slf4j.impl.JCLLoggerFactory.getLogger(JCLLoggerFactory.java:88)
in build.gradle file:
compile(group: "org.springframework", name: "spring-core", version: "3.0.5.RELEASE").Here version changed to 5.3.17
similar things applied for spring-context,spring-web etc spring related dependencies in build.gradle
Your SLF4J uses JCL as backend and your JCL uses SLF4J as backend, hence the StackOverflow.
Newer versions of Spring have spring-jcl as transitive dependency and they redirect logging coded using Jakarta Commons Logging to SLF4J.
If moreover you have a slf4j-jcl binding on your classpath that performs the opposite redirect, you get a StackOverflowException.
Just remove slf4j-jcl from your dependencies and you should be fine.

Unable to make field static final java.lang.invoke.MethodHandles$Lookup java.lang.invoke.MethodHandles$Lookup.IMPL_LOOKUP

I have a Spring Feign client which is sending POJO object to remote endpoint using POST and my application start-up fails with below exceptions.
java.lang.reflect.InaccessibleObjectException: Unable to make field static final java.lang.invoke.MethodHandles$Lookup java.lang.invoke.MethodHandles$Lookup.IMPL_LOOKUP accessible: module java.base does not "opens java.lang.invoke" to unnamed module #420a85c4
Below the dependencies I am using in my application.
java version: 17
spring boot version: 2.5.3
spring boot cloud version: 2020.0.3
spring boot starter openfeign version: 2.2.8.RELEASE
As recommended in https://github.com/OpenFeign/feign/issues/935, I had tried
workaround solution: Adding this jvm option '--add-opens java.base/java.lang.invoke=ALL-
UNNAMED' worked.
Any other alternative suggestion other than jvm argument are most welcome.
Force OpenFeign version to at least 11.7, which has this issue solved.
If you are using Spring Dependency Management plugin, then you can do it like this (Gradle example):
dependencyManagement {
dependencies {
dependencySet(group: 'io.github.openfeign', version: '11.7') {
entry 'feign-core'
entry 'feign-jackson'
entry 'feign-slf4j'
entry 'feign-soap'
entry 'feign-jaxb'
}
}
}
For me it worked to add the following JVM Option:
--add-opens java.base/java.lang.invoke=ALL-UNNAMED
For JDK 9+, if you use the JVM options to fix the problem, add another = into the JVM options:
e.g.
--add-opens=java.base/java.lang.invoke=ALL-UNNAMED
I have same error when i declare default method (method with implementation) in interface class marked with #FeignClient. I just remove default method and the error has disappeared

Error configuring mongo and neo4j with spring boot 1.5.7

I am using MongoDb and Neo4j in my Spring Boot Application. I have recently updated by Spring Boot Gradle Plugin from 1.2.6 to 1.5.7.
I am having two config files one for mongo and other for neo4j. After updating the version of spring boot I found that #EnableMongoRepositories and #EnableNeo4jRepositories are showing following errors in their respective config files:
No constructor with 1 argument defined in class
'org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean'
No constructor with 1 argument defined in class
'org.springframework.data.neo4j.repository.support.GraphRepositoryFactoryBean'
I am having following annotations in neo4j config:
#Configuration
#EnableNeo4jRepositories("<packagename>.neo4j.repository")
#EnableTransactionManagement
public class DatabaseConfigurationNeo4j
extends Neo4jConfiguration { ... }
and following annotations in mongo config:
#Configuration #Profile("!" + Constants.SPRING_PROFILE_CLOUD)
#EnableMongoRepositories("<packagename>.repository")
#Import(value = MongoAutoConfiguration.class)
#EnableMongoAuditing(auditorAwareRef = "springSecurityAuditorAware")
public class DatabaseConfiguration extends AbstractMongoConfiguration
{ ... }
If I remove these #EnableRepositories line from the files, these errors are removed but when I run it, the repositories bean are not creating. I think these lines are necessary but dont know how to remove this error.
Thank you.
updating spring-neo4j to 4.2.8 (not 5.0.0 as neo4j 5.0.0 will work with spring-boot 2.0 M and above) and spring-mongo to 1.10.6 solved the configuration issues.

Using Camel 2.1 with Grails 1.2.1 - Classloading problem

I'm trying to define a Camel context in my Grails application.
resource.groovy:
xmlns camel: 'http://camel.apache.org/schema/spring'
camel {
camelContext(id:'camelContext') {
}
}
Results in a stacktrace containing:
2010-02-03 13:24:42,209 [main] ERROR spring.GrailsRuntimeConfigurator - [RuntimeConfiguration] Unable to load beans from resources.groovy
org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.NoClassDefFoundError: org/apache/camel/spi/ManagementStrategy
The strange thing is that ManagementStrategy is in lib/camel-core-2.1.0.jar. I'm not that familiar with neither Spring nor Camel so any suggestions are welcome. Can this be a classloader issue?
Read the release notes for Apache Camel 2.1
http://camel.apache.org/camel-210-release.html
There is a section with new .jar dependencies.
You need commons-management .jar on the classpath.

Resources