Getting 405 - Method Not Allowed in Swagger (Spring Boot) - spring-boot

I'm getting a 405 - Method Not Allowed error when testing a GET method in Postman. In a browser, when trying to use swagger-ui I'm getting a Whitelabel Error Page.
I'm on Java 17, Spring Boot 2.7.3, and I'm using Swagger 3.0.
Here's the Swagger configuration. I've had to remove #Configuration because Spring Boot couldn't start when that annotation was in use and DocumentationType.OAS_30 is here instead of SWAGGER_2 because I'm using Swagger 3.0.
package hr.ogcs.blueprint;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
#EnableSwagger2
public class SwaggerConfig {
#Bean
public Docket api() {
return new Docket(DocumentationType.OAS_30).select()
.apis(RequestHandlerSelectors.basePackage("hr.ogcs.blueprint"))
.paths(PathSelectors.any()).build();
}
}
Here's a Gradle configuration. I've tried with springfox-boot-starter dependency, but the Spring Boot couldn't start with it. So I added swagger2 and swagger-ui dependencies.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'io.springfox:springfox-swagger2:3.0.0'
implementation 'io.springfox:springfox-swagger-ui:3.0.0'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

Here's a solution:
In order to solve this issue, I dropped the Springfox library and used another library named SpringDoc. The Springfox obviously still doesn't work with Swagger 3.0 so remove this from your Gradle:
implementation 'io.springfox:springfox-swagger2:3.0.0'
implementation 'io.springfox:springfox-swagger-ui:3.0.0'
The solution is quite simple.
Use this dependency instead, and everything will work like a charm:
`implementation` 'org.springdoc:springdoc-openapi-ui:1.6.11'
You don't need any additional Swagger Config file or any annotation. Now go to http://localhost:8080/swagger-ui.html to see the results.
I've followed this tutorial on Baeldung.

Related

Migrate #EnableCircuitBreaker and #EnableEurekaClient to Spring Cloud 2022.0.1

I upgraded Spring Cloud version to 2022.0.1 and I get error for imports not found:
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
Application class:
#EnableEurekaClient
#EnableCircuitBreaker
Do you know how I can properly import them or upgrade them?
since Spring boot 2.5.12 #EnableCircuitBreaker is Deprecated, So you don't need to use it.
Also, this happened to #EnableEurekaClient and there is no need to annotate it
Just add these two dependencies to the project
spring-cloud-starter-circuitbreaker-resilience4j
org.springframework.cloud:spring-cloud-starter-netflix-eureka-client

How do I use the OpenTelemetry component in Apache Camel?

Using the Spring Boot example from Camel, I'm trying to set up OpenTelemetry to instrument Camel.
Based on the docs, I chose the Spring Boot Auto-configuration option and added the dependency to my pom.xml.
I've also annotated the main class with #CamelOpenTelemetry:
package sample.camel;
import org.apache.camel.opentelemetry.starter.CamelOpenTelemetry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//CHECKSTYLE:OFF
/**
* A sample Spring Boot application that starts the Camel routes.
*/
#CamelOpenTelemetry
#SpringBootApplication
public class MyCamelApplication {
...
I started the application with OTEL_TRACES_EXPORTER=logging mvn spring-boot:run.
Is this correct? I'm not seeing any console output unlike this simple OpenTelemetry example.
I'm not sure if Camel 3.x is supported because this page says only 2.20+ is supported but Camel's docs says Since Camel 3.5 at the top.

ComponentScan does not find own custom annotations after update from Spring Boot 2.0.3 to 2.2.4

Prior to the update from Spring Boot 2.0.3 to 2.2.4 we had some annotations like the following example:
import org.springframework.stereotype.Component;
#Component
public #interface Adapter {
}
During ComponentScan classes annotated with this Adapter annotation got picked up and added to the Spring context. After the update there are now org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'XY' available.
According to a passage in the Terminology of the Spring Annotation Programming Model regarding Stereotype Annotations our approach should work:
Any component annotated with #Component is a candidate for component scanning. Similarly, any component annotated with an annotation that is itself meta-annotated with #Component is also a candidate for component scanning.
I read through the release notes of Spring Boot 2.1 and 2.2 and the release notes of the Spring Framework 5.1 and 5.2, but found nothing that the behaviour of stereotype annotations changed in this point.
When I annotate the classes with a Spring provided stereotype annotation (in addition to our own annotation) they get picked up by the ComponentScan.
I also tried to set a custom filter in a #ComponentScan like this
#ComponentScan(includeFilters = #ComponentScan.Filter(type = FilterType.ANNOTATION,
classes = Adapter.class))
but still got the NoSuchBeanDefinitionException
At last I stumbled upon this "Enhancement" in the Spring Framework and wonder if this could be the root of my problem.
So nothing worked so far and I would be glad if someone could point me in a direction that restores the original behaviour that our own stereotype annotations are working again. Thanks in advance.
After taking a closer look at the Spring Stereotype annotations adding #Retention(RetentionPolicy.RUNTIME) to our annotations solved the issue. The classes with our own annotations get picked up by ComponentScan again.
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.springframework.stereotype.Component;
#Retention(RetentionPolicy.RUNTIME)
#Component
public #interface Adapter {
}

Spring Boot doc is out of date

https://spring.io/guides/gs/spring-boot/
This is out of date as far as I can tell.
The github repo didn't exist so I used the initilizr mentioned beforehand and imported that into intellij.
then I tried creating a new java class like the first part of the tutorial says in https://spring.io/guides/gs/spring-boot/#_create_a_simple_web_application
but straight out the bat, the imports in the provided code fail :
package hello;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
#RestController
public class HelloController {
#RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
I googled around a bit and apparently (but I'm probably misunderstanding.) RestController is deprecated in favor of Controller?
what about RequestMapping then?
how do I set up a basic test rest service.
PS: in the initilizr (https://start.spring.io/), I chose Spring Boot 2.0.0 M6
in your pom.xml file, change : the very first <artifactId> line to :
<artifactId>spring-boot</artifactId>
it might have been something different (for me it was <artifactId>spring-boot-starter-parent</artifactId>).
with this you imports should be available.

Spring #Bean annotation differences (context vs config packages)?

What is the difference between these classes and when should I use each in conjunction with #Configure?
org.springframework.context.annotation.Bean
org.springframework.config.java.annotation.Bean;
It looks like you have an obsolete jar file in your classpath - org.springframework.config.java.annotation.Bean is no longer with the latest Spring 3.1.2 jars, it is the org.springframework.context.annotation.Bean annotation that should be used with the #Configuration classes
The JavaConfig dependency is part of the spring-context as of Spring 3.0. Therefore if you are building your app using Spring > 3.0 use the org.springframework.context.annotation.Bean

Resources