Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger UI with Java9 (spring boot 2.0)

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?

like image 987
Guigreg Avatar asked Jan 24 '26 14:01

Guigreg


2 Answers

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

like image 121
eis Avatar answered Jan 27 '26 07:01

eis


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.

like image 22
Madhavan Seshadri Avatar answered Jan 27 '26 05:01

Madhavan Seshadri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!