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

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

Related

Spring Boot 3 JSP Issue

I've been having issues with JSP since moving to Java 17 and Spring Boot 3. I know that we need to use jakarta.* instead of javax.*, but is there something I'm missing? I am using Spring Tools 4 and just running a basic web app using JSP. When using the following dependencies
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
</dependency>
The project runs, but I get the following error
The superclass "javax.servlet.http.HttpServlet", determined from the Dynamic Web Module facet version (2.5), was not found on the Java Build Path
I can get rid of it by adding the javax servlet dependency
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
But that goes against using javax.* dependencies with Spring Boot 3.
I have read these articles and tried adding the jakarta.servlet.jsp dependency with no luck.
https://debugah.com/solved-tomcat10-error-jakarta-servlet-servletexception-class-com-kuang-servlet-helloservlet-is-not-a-servlet-22749/
https://howtodoinjava.com/java/exception-handling/solved-the-superclass-javax-servlet-http-httpservlet-was-not-found-on-the-java-build-path-in-eclipse/
Solved!
All I had to do was go into Project Properties and under Project Faces, change my Dynamic Web Module from 2.5 to 5.0
The JSTL warning can be suppressed under Properties->Web->JSP Files->Validation->Custom actions->Other problems with TagExtraInfo class (Just change from 'Warning' to 'Ignore')

Azure AppConfiguration with Spring Boot 2.5.x

Question
How to integrate an Azure AppConfiguration with SpringBoot 2.5.x or higher?
Info
Im trying to use an Azure AppConfiguration resource with a Spring Boot 2.5.4 project. Unfortunately I cant get it to read a setting from the AppConfiguration or even connect to it as far as I can tell.
The project is newly created with the Spring Initializr where I only added
Spring Boot Starter Web
Spring Boot Starter Security
Spring Boot Starter WebSocket
Afterwards I tried following the Microsoft Quickstart documentation with no success. The documentation mentions that its using Spring 2.4.x so I assume some changes broke it.
I also tried to identify the issue by looking through some Azure Spring Boot Code Samples.
All the examples so far use the bootstrap.properties file which I learned during my search so far is deprecated. Moving the settings to the application.yml or enabling use-legacy-processing: true did not work either. Any ideas?
pom.xml
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</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>com.azure.spring</groupId>
<artifactId>azure-spring-cloud-appconfiguration-config</artifactId>
<version>2.0.0</version>
</dependency>
...
application.yml
spring:
config:
use-legacy-processing: true
profiles:
active: "develop"
application:
name: "MySampleService"
cloud:
azure:
appconfiguration:
stores:
- connection-string: "SomeAzureAppConfigurationResourceConnectionString"
label: ${spring.profiles.active}
#mysampleservice:
# message: "this is a message from file"
AppConfiguration Resource
Im not entirely sure about the format for the setting name. I tried to build the format based on this documentation.
The configuration classes should be fine since commenting in the mysampleservice causes the value of message beeing used.
Any hints are appreciated!
Some more info to elaborate on the accepted answer
The documentation linked in the answer refers to two different packages. The one linked right at the start in the maven repository is spring-cloud-azure-appconfiguration-config while the one used further down is azure-spring-cloud-appconfiguration-config. The second one works with the bootstrap.properties file.
Working pom.xml and bootstrap.properties:
...
<dependencies>
<!-- Dependency to load configuration from azure app configuration resource. Note that additional settings are required in bootstrap.properties
Documentation of settings: https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/appconfiguration/azure-spring-cloud-starter-appconfiguration-config
-->
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>azure-spring-cloud-appconfiguration-config-web</artifactId>
<version>2.1.0</version>
</dependency>
...
# Use this to enable or disable the cloud config, disabling it results in application.yaml beeing used.
spring.cloud.azure.appconfiguration.enabled=true
# Connection string to azure app configuration resource
spring.cloud.azure.appconfiguration.stores[0].connection-string= Endpoint=https://myofficeconfiguration.azconfig.io;Id=zUcT-l9-s0:PFYfW7WM0/Pz7WZOnH3v;Secret=JTB9myJqGekDAJ5m8Z1vjmkJZrPd88JbOEE3EqoqJYs=
# Configured filters for settings in the previous defined app configuration resource
spring.cloud.azure.appconfiguration.stores[0].selects[0].key-filter = /mysampleservice/
spring.cloud.azure.appconfiguration.stores[0].selects[0].label-filter = Sample
spring.cloud.azure.appconfiguration.stores[0].selects[1].key-filter = /notificationservice/
spring.cloud.azure.appconfiguration.stores[0].selects[1].label-filter = Sample2
bootstrap.yml/bootstrap.properties can still be used, they are no longer part of the base Spring packages.
Also, you want to use this doc for 2.0.0 and newer https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/appconfiguration/azure-spring-cloud-starter-appconfiguration-config.

java.lang.IllegalStateException: Unable to read meta-data for class org.springdoc.core.SwaggerUiConfigProperties error

I have a Spring boot application. Where I have added this below dependency for Swagger API specification.
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.4.3</version>
</dependency>
It is giving this following error:
java.lang.IllegalStateException: Unable to read meta-data for class
org.springdoc.core.SwaggerUiConfigProperties
Could anyone please help to sort this ? Before add this dependency , project worked without any issues.
You can add the the whole stacktrace, or create an issue on springdoc project with a sample minimal project to reproduce your error:
https://github.com/springdoc/springdoc-openapi/issues
Even though I have Build Success in Maven clean, It was giving error while maven install. When I fixed that issue, this issue also got solved.

Springboot 2.0.2 not working with thymeleaf template, throwing 404 error

I am using Springboot 1.5.7 for my rest api application, and I am using thymeleaf template to send emails from my api. But when I updated the version of spring boot to 2.0.2 its throwing 404 error i.e "Error resolving template "error", template might not exist or might not be accessible by any of the configured Template Resolvers".
Below is the config I have in application.yml
spring:
thymeleaf:
cache: false
enabled: true
mode: HTML5
prefix: /templates/
suffix: .html
thymeleaf version in pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
Below is the template structure i am using,
My app release is very near and I am badly stuck with this problem, if someone can provide me workaround then it would be a great help.
Remove prefix: /templates/ from application.yml
If still does not work, add thymeleaf-layout-dialect dependency (See: Thymeleaf stopped resolving layout templates after migrating to Thymeleaf 3)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
FYI, I put my views in WEB-INF/webapp/views, so I am using prefix: /WEB-INF/webapp/views/ (spring boot war deploy to tomcat)
As stated in the 2.0 migration guide.
The Thymeleaf starter included the thymeleaf-layout-dialect dependency
previously. Since Thymeleaf 3.0 now offers a native way to implement
layouts,
we removed that mandatory dependency and leave this choice up to you.
If your application is relying on the layout-dialect project, please
add it explicitly as a dependency.
Adding the following dependencies should work
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
SOURCE : https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide#template-engines

#Webservice annotation exception on weblogic

I am trying to run my application which contain JAX WS (2.1) Webservice using JDeveloper 11g R2(11.1.2.3.0) in JDK 1.6.0_31-b05. The error is coming from #WebService annotation present on the class.
When I am running the application, I am getting below error,
java.lang.IllegalArgumentException: Argument(s) "type" can't be null.
at com.sun.xml.bind.api.TypeReference.<init>(TypeReference.java:89)
at com.sun.xml.ws.model.RuntimeModeler.processDocWrappedMethod(RuntimeModeler.java:758)
at com.sun.xml.ws.model.RuntimeModeler.processMethod(RuntimeModeler.java:678)
at com.sun.xml.ws.model.RuntimeModeler.processClass(RuntimeModeler.java:428)
at com.sun.xml.ws.model.RuntimeModeler.buildRuntimeModel(RuntimeModeler.java:277)
at com.sun.xml.ws.server.EndpointFactory.createSEIModel(EndpointFactory.java:363)
at com.sun.xml.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:202)
at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:496)
at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:539)
at weblogic.wsee.jaxws.JAXWSDeployedServlet.getEndpoint(JAXWSDeployedServlet.java:183)
It seems that embedded Web Logic is using the internal libraries instead of provided one from JDK. The classes RuntimeModeler or TypeReference are present in JDK rt.jar starts with package com.sun.xml.ws.internal. Weblogic is picking these classes from glassfish.jaxb_1.0.0.0_2-1-12.jar & glassfish.jaxws.rt_1.2.0.0_2-1-5.jar, but these jars are not part of my application.
I have already used weblogic.xml with below tag,
<prefer-web-inf-classes>true</prefer-web-inf-classes>
I tried adding jaxws-api.jar & jws-api.jar in DefaultDomain/lib directory, but that didn't work
Any clue how to resolve this exception or how to force weblogic to use jdk runtime classes? The same application work properly on stand alone weblogic.
I had the same problem and found the answer here: http://www.intropro.com/resources/blog/66-argument-s-type-can-t-be-null
In short - the problem appears because you have jaxb-impl in you classpath which overrides WebLogics own jaxb, You may not explicitly refer to this dependency from your pom.xml, but some of your other dependencies do.
In my case I had apache-cxf as maven dependency and it had jaxb 2.1.13 as sub-dependency with scope "compile". All I had to do is exclude this apaches jaxb and add my own dependency with scope "provided" to explicitly use WebLogics jaxb.
in pom.xml it looked like this:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.7.2</version>
<exclusions>
<exclusion>
<artifactId>jaxb-impl</artifactId>
<groupId>com.sun.xml.bind</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<scope>provided</scope>
<version>2.1.13</version>
</dependency>
You can use eclipses "Dependency Hierarchy" tab in pom.xml view or simply command line "mvn dependency:tree" to find out how jaxb-impl made it to your classpath.
In my case, i had a typo in the arguments of the operation, where two arguments had the same webParam name. Modified that and deployed, issue was resolved.
Have you tried listing the correct jar in the manifest class-path: attribute? You could also put the jdk classes in the app and try using a FilteringClassLoader to specify which classes to use from the app rather than system classloader.
http://docs.oracle.com/cd/E15051_01/wls/docs103/programming/classloading.html#wp1097263

Resources