I am trying to implement circuit breaker in my spring boot app.
I made 2 bare minimum microservices to test the implementation of circuit breaker.
Here is my code.
Microservice A:
application.properties
server.port=8076
resilience4j.circuitbreaker.instances.recordstore.sliding-window-size=4
resilience4j.circuitbreaker.instances.recordstore.minimum-number-of-calls=2
resilience4j.circuitbreaker.instances.recordstore.failure-rate-threshold=70
resilience4j.circuitbreaker.instances.recordstore.automatic-transition-from-open-to-half-open-enabled=true
#resilience4j.circuitbreaker.instances.recordstore.enable-exponential-backoff=true
resilience4j.circuitbreaker.instances.recordstore.permitted-number-of-calls-in-half-open-state=2
resilience4j.circuitbreaker.instances.recordstore.sliding-window-type=COUNT_BASED
resilience4j.circuitbreaker.instances.recordstore.wait-duration-in-open-state=10
resilience4j.circuitbreaker.instances.recordstore.writable-stack-trace-enabled=true
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>A</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>A</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
<version>1.7.1</version>
</dependency>
<!-- <dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-timelimiter</artifactId>
<version>1.7.0</version> </dependency> -->
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-circuitbreaker</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
config
#Configuration
public class AConfig {
#Value("${resilience4j.circuitbreaker.instances.recordstore.minimum-number-of-calls}")
private int minimumNumberOfCalls;
#Value("${resilience4j.circuitbreaker.instances.recordstore.sliding-window-size}")
private int slidingWindowSize;
#Value("${resilience4j.circuitbreaker.instances.recordstore.sliding-window-type}")
private SlidingWindowType slidingWindowType;
#Value("${resilience4j.circuitbreaker.instances.recordstore.wait-duration-in-open-state}")
private int waitDurationInOpenState;
#Value("${resilience4j.circuitbreaker.instances.recordstore.failure-rate-threshold}")
private int failureRateThreshold;
#Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
#Bean
public CircuitBreaker circuitBreaker() {
CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
.slidingWindowSize(slidingWindowSize)
.slidingWindowType(slidingWindowType)
.waitDurationInOpenState(Duration.ofSeconds(waitDurationInOpenState))
.failureRateThreshold(failureRateThreshold)
.writableStackTraceEnabled(true)
.enableAutomaticTransitionFromOpenToHalfOpen()
.automaticTransitionFromOpenToHalfOpenEnabled(true)
.build();
CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.of(circuitBreakerConfig);
return circuitBreakerRegistry.circuitBreaker("recordstore", circuitBreakerConfig) ;
}
}
controller
#RestController
public class AController {
private static final Logger LOGGER = LoggerFactory.getLogger(AController.class);
#Autowired
RestTemplate restTemplate;
#Autowired
CircuitBreaker circuitBreaker;
#GetMapping("/test/{id}")
public void test(#PathVariable Integer id) {
System.out.println(" id :::" + id);
Supplier<String> supplier = () -> restTemplate.getForObject("http://localhost:8077/read", String.class);
Supplier<String> decoratedSupplier = circuitBreaker.decorateSupplier(supplier);
Try<String> response = Try.ofSupplier(decoratedSupplier).recover(throwable -> this.logId(id));
LOGGER.info("Response for id::: {} ...is.... {}" , id, response);
}
private String logId(Integer id) {
System.out.println("Logging id for RTB");
return "manual intervention needed";
}
}
Below is code of Microservice B:
#RestController
public class BController {
#GetMapping("/read")
public String read() {
System.out.println("in B");
return "Successsssssss";
}
}
Here are my logs. Initially microservice B is Up and I fire 3 request after that I bring microservice B down and fire another 6 requests.
2021-07-17 17:31:33.197 INFO 9904 --- [ main] com.example.demo.AApplication : Started AApplication in 3.808 seconds (JVM running for 4.722)
2021-07-17 17:31:53.562 INFO 9904 --- [nio-8076-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-07-17 17:31:53.562 INFO 9904 --- [nio-8076-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-07-17 17:31:53.564 INFO 9904 --- [nio-8076-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
id :::1
2021-07-17 17:31:53.948 INFO 9904 --- [nio-8076-exec-1] com.example.demo.AController : Response for id::: 1 ...is.... Success(Successsssssss)
id :::1
2021-07-17 17:31:57.466 INFO 9904 --- [nio-8076-exec-2] com.example.demo.AController : Response for id::: 1 ...is.... Success(Successsssssss)
id :::1
2021-07-17 17:32:11.553 INFO 9904 --- [nio-8076-exec-3] com.example.demo.AController : Response for id::: 1 ...is.... Success(Successsssssss)
id :::1
Logging id for RTB
2021-07-17 17:32:21.132 INFO 9904 --- [nio-8076-exec-4] com.example.demo.AController : Response for id::: 1 ...is.... Success(manual intervention needed)
id :::2
Logging id for RTB
2021-07-17 17:32:27.915 INFO 9904 --- [nio-8076-exec-5] com.example.demo.AController : Response for id::: 2 ...is.... Success(manual intervention needed)
id :::3
Logging id for RTB
2021-07-17 17:32:38.345 INFO 9904 --- [nio-8076-exec-6] com.example.demo.AController : Response for id::: 3 ...is.... Success(manual intervention needed)
id :::4
Logging id for RTB
2021-07-17 17:32:42.706 INFO 9904 --- [nio-8076-exec-7] com.example.demo.AController : Response for id::: 4 ...is.... Success(manual intervention needed)
id :::5
Logging id for RTB
2021-07-17 17:32:47.001 INFO 9904 --- [nio-8076-exec-8] com.example.demo.AController : Response for id::: 5 ...is.... Success(manual intervention needed)
id :::6
Logging id for RTB
2021-07-17 17:32:56.484 INFO 9904 --- [nio-8076-exec-9] com.example.demo.AController : Response for id::: 6 ...is.... Success(manual intervention needed)
I have invested 4 days now and am unable to understand why this simple thing is not working. Why does my circuit not open. I am feeling extreemly demotivated. Please someone who knows this help me. I will be really thankful to you.
Related
I'm trying to retrieve data from an Oracle Database table, and I'm new to Apache Spark.
I must retrieve data from two databases with two tables (production DB and lower environment)
must compare the data in both tables column by column and then insert it into another audit table in a lower environment. Later, a job will update the lower environment with the updated data.
Due to the enormous amount of data, many tables with various schemas will contain trillions of records.
The problem is that I cannot connect to and retrieve records from the database.
Any possible solution would be greatly appreciated. Thanks
public static void main(String[] args) {
SpringApplication.run(ApacheSparkApplication.class, args);
SparkSession spark = SparkSession.builder().master("local[*]").appName("SparkandOracledbTest").getOrCreate();
Dataset<Row> oracleDF = spark.read().format("oracle")
.option("adbId", "jdbc:oracle:thin:#host:port/dbname")
.option("dbtable", "owner.tablename")
.option("user", "user")
.option("password", "password")
.load();
oracleDF.show();
2022-10-26 08:25:22.557 INFO 5448 --- [ restartedMain] o.s.jetty.server.handler.ContextHandler : Started o.s.j.s.ServletContextHandler#244c93d7{/metrics/json,null,AVAILABLE,#Spark}
2022-10-26 08:25:22.765 WARN 5448 --- [ restartedMain] o.apache.spark.sql.internal.SharedState : URL.setURLStreamHandlerFactory failed to set FsUrlStreamHandlerFactory
2022-10-26 08:25:22.766 INFO 5448 --- [ restartedMain] o.apache.spark.sql.internal.SharedState : Setting hive.metastore.warehouse.dir ('null') to the value of spark.sql.warehouse.dir.
2022-10-26 08:25:22.776 INFO 5448 --- [ restartedMain] o.apache.spark.sql.internal.SharedState : Warehouse path is 'file:/C:/Vijay/csv/spark-warehouse'.
2022-10-26 08:25:22.793 INFO 5448 --- [ restartedMain] o.s.jetty.server.handler.ContextHandler : Started o.s.j.s.ServletContextHandler#7b364643{/SQL,null,AVAILABLE,#Spark}
2022-10-26 08:25:22.794 INFO 5448 --- [ restartedMain] o.s.jetty.server.handler.ContextHandler : Started o.s.j.s.ServletContextHandler#76e99147{/SQL/json,null,AVAILABLE,#Spark}
2022-10-26 08:25:22.795 INFO 5448 --- [ restartedMain] o.s.jetty.server.handler.ContextHandler : Started o.s.j.s.ServletContextHandler#40a15ec9{/SQL/execution,null,AVAILABLE,#Spark}
2022-10-26 08:25:22.796 INFO 5448 --- [ restartedMain] o.s.jetty.server.handler.ContextHandler : Started o.s.j.s.ServletContextHandler#d385186{/SQL/execution/json,null,AVAILABLE,#Spark}
2022-10-26 08:25:22.798 INFO 5448 --- [ restartedMain] o.s.jetty.server.handler.ContextHandler : Started o.s.j.s.ServletContextHandler#3fa6b59a{/static/sql,null,AVAILABLE,#Spark}
Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.NoClassDefFoundError: org/codehaus/janino/InternalCompilerException
at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSource(DataSource.scala:675)
at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSourceV2(DataSource.scala:725)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:207)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:171)
at com.spark.csv.ApacheSparkApplication.main(ApacheSparkApplication.java:34)
... 5 more
Caused by: java.lang.ClassNotFoundException: org.codehaus.janino.InternalCompilerException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 10 more
POM.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artif`actId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.13</artifactId>
<version>3.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.13</artifactId>
<version>3.3.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.codehaus.janino/janino -->
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.codehaus.janino/commons-compiler -->
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>commons-compiler</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.oracle.ojdbc/ojdbc8 -->
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.3.0.0</version>
</dependency>
</dependencies>
I'm learning Apache Camel in Spring Boot project and I try to create a Retful Webservice and the service is starting but the problem is that I get 404 when I call the endpoint.
#Component
#RequiredArgsConstructor
public class RestJavaDsl extends RouteBuilder {
private final WeatherDataProvider weatherDataProvider;
#Override
public void configure() throws Exception {
from("rest:get:javadsl/weather/{city}?produces=application/json")
.outputType(WeatherDto.class)
.process(this::getWeatherData);
}
private void getWeatherData(Exchange exchange) {
String city = exchange.getMessage().getHeader("city", String.class);
WeatherDto currentWeather = weatherDataProvider.getCurrentWeather(city);
Message message = new DefaultMessage(exchange.getContext());
message.setBody(currentWeather);
exchange.setMessage(message);
}
}
I created this class to hardcode some data:
#Component
public class WeatherDataProvider {
private static Map<String, WeatherDto> weatherData = new HashMap<>();
public WeatherDataProvider() {
WeatherDto dto = WeatherDto.builder().city("London").temp("10").unit("C").receivedTime(new Date().toString()).id(1).build();
weatherData.put("LONDON", dto);
}
public WeatherDto getCurrentWeather(String city) {
return weatherData.get(city.toUpperCase());
}
public void setCurrentWeather(WeatherDto dto) {
dto.setReceivedTime(new Date().toString());
weatherData.put(dto.getCity().toUpperCase(), dto);
}
}
#Data
#NoArgsConstructor
#AllArgsConstructor
#Builder
public class WeatherDto implements Serializable {
static int counter = 1;
private int id = counter++;
private String city;
private String temp;
private String unit;
private String receivedTime;
}
application.yml
camel:
component:
servlet:
mapping:
context-path: /services/*
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dgs</groupId>
<artifactId>camel-rest-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>camel-rest-springboot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<camel.version>3.14.0</camel.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test</artifactId>
<scope>test</scope>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jaxb</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-servlet</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-servlet-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-rest-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The serviceis starting and this is the console log:
2022-01-24 20:01:40.353 INFO 15796 --- [ main] c.d.c.CamelRestSpringbootApplication : Starting CamelRestSpringbootApplication using Java 11.0.5 on pc-PC with PID 15796 (D:\Spring Boot\camel-rest-springboot\target\classes started by pc in D:\Spring Boot\camel-rest-springboot)
2022-01-24 20:01:40.357 INFO 15796 --- [ main] c.d.c.CamelRestSpringbootApplication : No active profile set, falling back to default profiles: default
2022-01-24 20:01:43.583 INFO 15796 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-01-24 20:01:43.604 INFO 15796 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-01-24 20:01:43.604 INFO 15796 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-01-24 20:01:43.820 INFO 15796 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-01-24 20:01:43.821 INFO 15796 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3235 ms
2022-01-24 20:01:45.228 INFO 15796 --- [ main] o.a.c.c.s.CamelHttpTransportServlet : Initialized CamelHttpTransportServlet[name=CamelServlet, contextPath=]
2022-01-24 20:01:45.233 INFO 15796 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-01-24 20:01:45.592 INFO 15796 --- [ main] o.a.c.impl.engine.AbstractCamelContext : Message DataType is enabled on CamelContext: camel-1
2022-01-24 20:01:45.607 INFO 15796 --- [ main] o.a.c.impl.engine.AbstractCamelContext : Routes startup (total:1 started:1)
2022-01-24 20:01:45.607 INFO 15796 --- [ main] o.a.c.impl.engine.AbstractCamelContext : Started route1 (rest://get:javadsl/weather/%7Bcity%7D)
2022-01-24 20:01:45.607 INFO 15796 --- [ main] o.a.c.impl.engine.AbstractCamelContext : Apache Camel 3.14.0 (camel-1) started in 370ms (build:83ms init:269ms start:18ms)
2022-01-24 20:01:45.617 INFO 15796 --- [ main] c.d.c.CamelRestSpringbootApplication : Started CamelRestSpringbootApplication in 6.569 seconds (JVM running for 8.087)
But when I try to call the endpoint http://localhost:8080/services/javadsl/weather/london
It looks like this endpoint doesn't exist, the rest isn't created. I used debugger and the method getWeatherData() isn't called.
And I think this log is not ok: Started route1 (rest://get:javadsl/weather/%7Bcity%7D), it should be something like that: route1 started and consuming from servlet:/javadsl/weather/%7Bcity%7D
And the tutorial is from here: https://www.youtube.com/watch?v=spDjbC8mZf0&t=433s
Thank you in advance!
You can start by creating example project using official camel-archetype-spring-boot maven archetype. You can generate project based on the archetype using your IDE or from command-line using following maven command.
mvn archetype:generate -DarchetypeArtifactId="camel-archetype-spring-boot" -DarchetypeGroupId="org.apache.camel.archetypes" -DarchetypeVersion="3.14.0"
Now in the maven project file pom.xml add following block to the dependencies
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jetty-starter</artifactId>
</dependency>
Note that version doesn't need to be specified as it is provided by camel-spring-boot-dependencies BOM (bill of materials) in dependencyManagement section.
After that you can create new file ConfigureCamelContext.java where we can configure our CamelContext and provide it with RestConfiguration. We can do this by implementing CamelContextConfiguration and #Component annotation.
The annotation tells spring-framework that it should create instance of the class and inject it to objects that request it based on its class-type or interface. Camel is configured to automatically ask spring-framework for these RestConfiguration instances which it will proceed to use configure CamelContext.
package com.example;
import org.apache.camel.CamelContext;
import org.apache.camel.spi.RestConfiguration;
import org.apache.camel.spring.boot.CamelContextConfiguration;
import org.springframework.stereotype.Component;
#Component
public class ConfigureCamelContext implements CamelContextConfiguration {
#Override
public void beforeApplicationStart(CamelContext camelContext) {
RestConfiguration restConfiguration = new RestConfiguration();
restConfiguration.setApiComponent("jetty");
restConfiguration.setApiHost("localhost");
restConfiguration.setPort(8081);
camelContext.setRestConfiguration(restConfiguration);
}
#Override
public void afterApplicationStart(CamelContext camelContext) {
}
}
This should work with RestDSL and rest-component.
Edit MySpringBootRouter.java to something like this:
package com.example;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
#Component
public class MySpringBootRouter extends RouteBuilder {
static final String CONTET_TYPE_TEXT = "text/plain";
#Override
public void configure() {
rest()
.get("/hello/")
.route()
.setHeader(Exchange.CONTENT_TYPE, constant(CONTET_TYPE_TEXT))
.setBody().constant("Hello world")
.end()
.endRest()
.get("/hello/{name}")
.route()
.setHeader(Exchange.CONTENT_TYPE, constant(CONTET_TYPE_TEXT))
.setBody().simple("Hello ${headers.name}")
.end()
.endRest();
from("rest:get:test?produces=plain/text")
.setBody().constant("Hello from JavaDSL");
}
}
Comment out the class under src/test/java/<grouId>/ as the example test provided by the archetype is not applicable for for MySpringBootRouter and will interrupt build.
To run the project you can use command
mvn spring-boot:run
Now you should be able to open up localhost:8081/hello, localhost:8081/hello/<Name> or localhost:8081/test get response in plain text.
I was using a tutorial and everything was working fine until I started dealing with swagger 2 dependencies.
I wonder now if there is a way to fix this.
SwaggerConfig:
package com.animes.apirest.config;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import static springfox.documentation.builders.PathSelectors.regex;
import java.util.ArrayList;
#Configuration
#EnableSwagger2
public class SwaggerConfig {
#Bean
public Docket atividadeApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.atividades.apirest"))
.paths(regex("/api.*"))
.build()
.apiInfo(metaInfo());
}
private ApiInfo metaInfo() {
ApiInfo apiInfo = new ApiInfo(
"Atividades API REST",
"API REST de cadastro de atividades.",
"1.0",
"Terms of Service",
new Contact("João VR", "www.una.br/",
" "),
"Apache License Version 2.0",
"https://www.apache.org/licesen.html", new ArrayList<VendorExtension>()
);
return apiInfo;
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.animes</groupId>
<artifactId>apirest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>apirest</name>
<description>Anime project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Error:
19:07:08.137 [Thread-0] DEBUG org.springframework.boot.devtools.restart.classloader.RestartClassLoader - Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader#43899316
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.0)
2021-11-21 19:07:08.606 INFO 9840 --- [ restartedMain] com.animes.apirest.ApirestApplication : Starting ApirestApplication using Java 16.0.2 on DESKTOP-TIGCP3C with PID 9840 (C:\Program Files (x86)\eclipse\Workspace\apirest\target\classes started by Pichau in C:\Program Files (x86)\eclipse\Workspace\apirest)
2021-11-21 19:07:08.607 INFO 9840 --- [ restartedMain] com.animes.apirest.ApirestApplication : No active profile set, falling back to default profiles: default
2021-11-21 19:07:08.679 INFO 9840 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2021-11-21 19:07:08.680 INFO 9840 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2021-11-21 19:07:09.807 INFO 9840 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-11-21 19:07:09.895 INFO 9840 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 71 ms. Found 1 JPA repository interfaces.
2021-11-21 19:07:10.790 INFO 9840 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-11-21 19:07:10.813 INFO 9840 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-11-21 19:07:10.813 INFO 9840 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.55]
2021-11-21 19:07:10.965 INFO 9840 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-11-21 19:07:10.966 INFO 9840 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2286 ms
2021-11-21 19:07:11.194 INFO 9840 --- [ restartedMain] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-11-21 19:07:11.245 INFO 9840 --- [ restartedMain] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.1.Final
2021-11-21 19:07:11.427 INFO 9840 --- [ restartedMain] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-11-21 19:07:11.536 INFO 9840 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-11-21 19:07:11.746 INFO 9840 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2021-11-21 19:07:11.776 INFO 9840 --- [ restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL10Dialect
2021-11-21 19:07:12.466 INFO 9840 --- [ restartedMain] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-11-21 19:07:12.475 INFO 9840 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-11-21 19:07:13.074 WARN 9840 --- [ restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-11-21 19:07:13.266 INFO 9840 --- [ restartedMain] pertySourcedRequestMappingHandlerMapping : Mapped URL path [/v2/api-docs] onto method [springfox.documentation.swagger2.web.Swagger2Controller#getDocumentation(String, HttpServletRequest)]
2021-11-21 19:07:13.497 INFO 9840 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2021-11-21 19:07:13.717 INFO 9840 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-11-21 19:07:13.718 INFO 9840 --- [ restartedMain] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
2021-11-21 19:07:13.740 INFO 9840 --- [ restartedMain] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
2021-11-21 19:07:13.744 WARN 9840 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException: Cannot invoke "org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.toString()" because the return value of "springfox.documentation.spi.service.contexts.Orderings.patternsCondition(springfox.documentation.RequestHandler)" is null
2021-11-21 19:07:13.747 INFO 9840 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2021-11-21 19:07:13.751 INFO 9840 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2021-11-21 19:07:13.760 INFO 9840 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
2021-11-21 19:07:13.782 INFO 9840 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2021-11-21 19:07:13.796 INFO 9840 --- [ restartedMain] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-11-21 19:07:13.826 ERROR 9840 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException: Cannot invoke "org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.toString()" because the return value of "springfox.documentation.spi.service.contexts.Orderings.patternsCondition(springfox.documentation.RequestHandler)" is null
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-5.3.13.jar:5.3.13]
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.13.jar:5.3.13]
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.3.13.jar:5.3.13]
at java.base/java.lang.Iterable.forEach(Iterable.java:75) ~[na:na]
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[spring-context-5.3.13.jar:5.3.13]
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[spring-context-5.3.13.jar:5.3.13]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) ~[spring-context-5.3.13.jar:5.3.13]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.13.jar:5.3.13]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.0.jar:2.6.0]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) ~[spring-boot-2.6.0.jar:2.6.0]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412) ~[spring-boot-2.6.0.jar:2.6.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) ~[spring-boot-2.6.0.jar:2.6.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) ~[spring-boot-2.6.0.jar:2.6.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1290) ~[spring-boot-2.6.0.jar:2.6.0]
at com.animes.apirest.ApirestApplication.main(ApirestApplication.java:10) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.6.0.jar:2.6.0]
Caused by: java.lang.NullPointerException: Cannot invoke "org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.toString()" because the return value of "springfox.documentation.spi.service.contexts.Orderings.patternsCondition(springfox.documentation.RequestHandler)" is null
at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:112) ~[springfox-spi-2.9.2.jar:null]
at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:109) ~[springfox-spi-2.9.2.jar:null]
at com.google.common.collect.ComparatorOrdering.compare(ComparatorOrdering.java:37) ~[guava-20.0.jar:na]
at java.base/java.util.TimSort.countRunAndMakeAscending(TimSort.java:355) ~[na:na]
at java.base/java.util.TimSort.sort(TimSort.java:220) ~[na:na]
at java.base/java.util.Arrays.sort(Arrays.java:1232) ~[na:na]
at com.google.common.collect.Ordering.sortedCopy(Ordering.java:855) ~[guava-20.0.jar:na]
at springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider.requestHandlers(WebMvcRequestHandlerProvider.java:57) ~[springfox-spring-web-2.9.2.jar:null]
at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:138) ~[springfox-spring-web-2.9.2.jar:null]
at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:135) ~[springfox-spring-web-2.9.2.jar:null]
at com.google.common.collect.Iterators$7.transform(Iterators.java:750) ~[guava-20.0.jar:na]
at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:47) ~[guava-20.0.jar:na]
at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:47) ~[guava-20.0.jar:na]
at com.google.common.collect.MultitransformedIterator.hasNext(MultitransformedIterator.java:52) ~[guava-20.0.jar:na]
at com.google.common.collect.MultitransformedIterator.hasNext(MultitransformedIterator.java:50) ~[guava-20.0.jar:na]
at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:249) ~[guava-20.0.jar:na]
at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:209) ~[guava-20.0.jar:na]
at com.google.common.collect.FluentIterable.toList(FluentIterable.java:614) ~[guava-20.0.jar:na]
at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.defaultContextBuilder(DocumentationPluginsBootstrapper.java:111) ~[springfox-spring-web-2.9.2.jar:null]
at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.buildContext(DocumentationPluginsBootstrapper.java:96) ~[springfox-spring-web-2.9.2.jar:null]
at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:167) ~[springfox-spring-web-2.9.2.jar:null]
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) ~[spring-context-5.3.13.jar:5.3.13]
... 19 common frames omitted
Searching about I tried to change versions to 2.8.0, 2.7.0, 3.0.0... also returns error.
The application is an apirest with task list activities.
try upgrade version of springfox, add spring fox starter and remove #EnableSwagger2
Dependencies to be added
<!--springfox dependencies for api documentations in swagger -->
<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>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
Remove anotations #EnableSwagger2
#Configuration
// #EnableSwagger2 // remove this annotation
public class SwaggerConfig { ... }
try on this link if there is no override changing in default path
http://localhost:{port}/swagger-ui/index.html
I have solved this issue in the Latest Spring Boot 2.6.7 version by following steps. Anyone can check who is facing this issue in 2.6.7 version.
Comment/Remove actuator dependency in pom.xml file
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-actuator</artifactId>-->
<!-- </dependency>-->
Add swagger dependency in pom.xml file
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
Swagger Configuration class change to
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
#Configuration
#EnableWebMvc
public class SwaggerConfig implements WebMvcConfigurer {
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage("com.companyname.app"))
.paths(PathSelectors.regex("/.*"))
.build().apiInfo(apiInfoMetaData());
}
private ApiInfo apiInfoMetaData() {
return new ApiInfoBuilder().title("NAME OF SERVICE")
.description("API Endpoint Decoration")
.contact(new Contact("Dev-Team", "https://www.dev-team.com/", "dev-team#gmail.com"))
.license("Apache 2.0")
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
.version("1.0.0")
.build();
}
}
Swagger apis URL
My case
http://localhost:8080/swagger-ui/index.html
Your case if you different port and project context path
http://localhost:{port}/your-context-path/swagger-ui/index.html
The springfox plugin is not compatible with PathPattern-based matching in Spring MVC which has replaced the previous Ant-based matching in the Spring Boot default settings. See e.g. here
https://stackoverflow.com/a/70037507/15496965
https://stackoverflow.com/a/69814964/15496965
You can try to set spring.mvc.pathmatch.matching-strategy=ant_path_matcher to flip that property back to its previous default value. But this won't help if you use actuators which are not effected by that property.
You can try to force the actuators back to Ant-based matching as explained in the second post. But I'd really not recommend that. Instead, you can use Spring Boot <= 2.5 for the moment or migrate to springdoc.
I know this does not solve your problem directly, but consider moving to springdoc. Springfox is so buggy at this point that is a pain to use. I've moved to springdoc 2 years ago because of its Spring WebFlux support and I am very happy about it. Additionally, it also supports Kotlin Coroutines, which I am not sure Springfox does.
If you decide to migrate, springdoc even has a migration guide.
Additionally, you are already using Spring Boot 2.6, which is very very recent (its release notes are from a couple of hours ago) and thus springdoc and Springfox might not support it already. So I would also suggest you use Spring Boot 2.5.7 instead.
Just add this configuration to your application.properties
spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER
then Run your application.
You need to edit one part, the base package name. As shown in the screenshot my base package name.
my package name
#Configuration
#EnableSwagger2
public class SwaggerConfig2 {
#Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(true)
.apiInfo(new ApiInfoBuilder()
.title("Swagger Super")
.description("Swagger Description details")
.version("1.0").build())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
.paths(PathSelectors.any()).build();
}
}
OR
#Configuration
#EnableSwagger2
public class SwaggerConfig2 {
#Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(true)
.apiInfo(new ApiInfoBuilder()
.title("Swagger Super")
.description("Swagger Description details")
.version("1.0").build())
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.paths(PathSelectors.any()).build();
}
}
And the Swagger Dependencies
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
Try like this, I hope it will work
Note: SNAPSHOT, M1, M2, M3, and M4 releases typically WORK IN PROGRESS. The Spring team is still working on them, Recommend NOT using them.
It should be added, when i add it resolved
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
show the below all swagger dependency
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-schema</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
I had the same error, but I get down the org.springframework.boot to 2.5.8, and I had it on 2.7.1.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
And the Swagger Dependencies
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
And It works!!!
Add #Enablewebmvc in #Configuration class
and remove all other dependencies and include only below one for swagger
Add #Enablewebmvc in #Configuration class
and remove all other dependencies and include only below one for swagger
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency> "
Just adding below to your application.properties works like magic
spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER
I created a very basic Spring Boot project with REST APIs. I tried connecting it to my Angular app but it was getting some CORS security error so I switched to Postman. I'm trying to test it using Postman but I keep receiving a 404 not found error on Postman. Why am I not able to connect to my backend and post to tester function?
controller
package controllers;
#RestController
public class Handler {
#PostMapping("/tester/{id}")
public String tester(#PathVariable(value="id")long userid ){
System.out.println("in tester");
System.out.println(userid);
return "succeeded test";
}
}
main
package halloween.contest;
#SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,
SecurityAutoConfiguration.class})
public class ContestApplication {
public static void main(String[] args) {
SpringApplication.run(ContestApplication.class, args);
}
}
application.properties
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration
spring.data.rest.base-path=/
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>halloween</groupId>
<artifactId>contest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>contest</name>
<description>halloween picture contest</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
console
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.6)
2021-10-26 15:44:12.823 INFO 18760 --- [ main] halloween.contest.ContestApplication : Starting ContestApplication using Java 1.8.0_144 on DESKTOP-VB80TS0 with PID 18760 (C:\Users\Sergio Rodríguez\Documents\Halloween\contest\target\classes started by Sergio Rodríguez in C:\Users\Sergio Rodríguez\Documents\Halloween\contest)
2021-10-26 15:44:12.827 INFO 18760 --- [ main] halloween.contest.ContestApplication : No active profile set, falling back to default profiles: default
2021-10-26 15:44:16.013 INFO 18760 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-10-26 15:44:16.043 INFO 18760 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-10-26 15:44:16.043 INFO 18760 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.54]
2021-10-26 15:44:16.310 INFO 18760 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-10-26 15:44:16.310 INFO 18760 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3372 ms
2021-10-26 15:44:19.326 INFO 18760 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-10-26 15:44:19.342 INFO 18760 --- [ main] halloween.contest.ContestApplication : Started ContestApplication in 7.201 seconds (JVM running for 7.946)
2021-10-26 15:45:45.138 INFO 18760 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-10-26 15:45:45.139 INFO 18760 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-10-26 15:45:45.157 INFO 18760 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 17 ms
equivalent cURL command
curl --location --request POST 'localhost:8080/tester/69'
Postman
Answering my own question now that I figured it out. It wasn't apparent based on the code I provided but I'll leave my post for future help (wording helps).
the problem was visibility of packages. I had my controller class in a different package from the main class.
To fix the CORS issue, next to #PostMapping("/tester/{id}") add #CrossOrigin(origins = "http://localhost:<port>") where port is the port from which your Angular app is running.
In your target URL, make sure you've included the default Spring Boot port of 8080, e.g. http://localhost:8080/tester/123 and obviously, please double check that you're doing a POST and not a GET.
got a similar problem..then added the below annotations on top of springbootappication where main() exists, then it's working fine for me.
#EnableWebMvc
#ComponentScan("com.*.*")
Also in postman make the below changes...
Disable/uncheck File --> Proxy --> "Use the system proxy"
Disable/uncheck File --> General --> "SSL Certificate Verification"
I am implementing Cosmos DB in my Spring Boot project, and am getting error while connecting to Azure Cosmos DB.
POM.xml
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-cosmosdb-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>spring-data-cosmosdb</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-documentdb</artifactId>
<version>2.4.6</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
Repository
package com.staples.ctg.repository.cosmos;
import org.springframework.stereotype.Repository;
import com.microsoft.azure.spring.data.cosmosdb.repository.ReactiveCosmosRepository;
import com.staples.ctg.service.dto.cache.GenericCacheDTO;
#Repository
public interface SpotCacheRepository extends ReactiveCosmosRepository <GenericCacheDTO, String> {
}
applicationProperties.yml
azure:
cosmosdb:
uri: Cosmos URI
key: Key
database: DB Name that I created
DTO
package com.staples.ctg.service.dto.cache;
#Document(collection="cosmos")
public class GenericCacheDTO {
#Id
private String id;
#PartitionKey
private String accountId;
}
When I spin up locally, I am getting below error
2020-02-14 12:20:32.922 INFO 22777 --- [ restartedMain] c.a.d.c.internal.RxDocumentClientImpl : Initializing DocumentClient with serviceEndpoint [<Azure Cosmos URI>], connectionPolicy [ConnectionPolicy{requestTimeoutInMillis=60000, mediaRequestTimeoutInMillis=300000, connectionMode=DIRECT, maxPoolSize=1000, idleConnectionTimeoutInMillis=60000, userAgentSuffix=';spring-data/2.2.0;8d0607104487ba8c93ae7a32c5e20645d1bb21edf269625ead71da72d4877d65;', retryOptions=RetryOptions{maxRetryAttemptsOnThrottledRequests=9, maxRetryWaitTimeInSeconds=30}, enableEndpointDiscovery=true, preferredLocations=null, usingMultipleWriteLocations=false, inetSocketProxyAddress=null}], consistencyLevel [null], directModeProtocol [Tcp]
2020-02-14 12:20:32.922 DEBUG 22777 --- [ restartedMain] reactor.util.Loggers$LoggerFactory : Using Slf4j logging framework
2020-02-14 12:20:32.979 DEBUG 22777 --- [ restartedMain] c.a.d.c.internal.GlobalEndpointManager : registering a refresh in [300000] ms
2020-02-14 12:20:33.034 DEBUG 22777 --- [ parallel-1] c.a.d.c.internal.GlobalEndpointManager : startRefreshLocationTimerAsync() - Invoking refresh, I was registered on [2020-02-14T12:20:32.985]
2020-02-14 12:20:33.035 INFO 22777 --- [ parallel-1] c.a.d.c.internal.RxDocumentClientImpl : Getting database account endpoint from https:<Azure Cosmos URI>
2020-02-14 12:20:33.123 DEBUG 22777 --- [ parallel-1] reactor.netty.tcp.TcpResources : [http] resources will use the default LoopResources: DefaultLoopResources {prefix=reactor-http, daemon=true, selectCount=8, workerCount=8}
2020-02-14 12:20:33.123 DEBUG 22777 --- [ parallel-1] reactor.netty.tcp.TcpResources : [http] resources will use the default ConnectionProvider: PooledConnectionProvider {name=http, poolFactory=reactor.netty.resources.ConnectionProvider$$Lambda$2068/965477132#65222f44}
2020-02-14 12:20:33.130 DEBUG 22777 --- [ parallel-1] r.netty.resources.DefaultLoopEpoll : Default Epoll support : false
2020-02-14 12:20:33.132 DEBUG 22777 --- [ parallel-1] r.netty.resources.DefaultLoopKQueue : Default KQueue support : false
2020-02-14 12:20:33.162 DEBUG 22777 --- [ parallel-1] r.n.resources.PooledConnectionProvider : Creating new client pool [reactor-netty-connection-pool] for <Azure Cosmos URI>
2020-02-14 12:20:33.183 ERROR 22777 --- [ parallel-1] reactor.core.scheduler.Schedulers : Scheduler worker in group main failed with an uncaught exception
java.lang.NoSuchMethodError: reactor.util.concurrent.Queues.empty()Ljava/util/function/Supplier;
at reactor.netty.internal.shaded.reactor.pool.SimpleFifoPool.<clinit>(SimpleFifoPool.java:35)
at reactor.netty.internal.shaded.reactor.pool.PoolBuilder.build(PoolBuilder.java:333)
at reactor.netty.internal.shaded.reactor.pool.PoolBuilder.fifo(PoolBuilder.java:321)
at reactor.netty.resources.ConnectionProvider.lambda$fixed$3(ConnectionProvider.java:197)
at reactor.netty.resources.PooledConnectionProvider$PooledConnectionAllocator.<init>(PooledConnectionProvider.java:237)
at reactor.netty.resources.PooledConnectionProvider.lambda$acquire$2(PooledConnectionProvider.java:154)
at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
at reactor.netty.resources.PooledConnectionProvider.lambda$acquire$3(PooledConnectionProvider.java:148)
at reactor.core.publisher.MonoCreate.subscribe(MonoCreate.java:54)
at reactor.netty.http.client.HttpClientConnect$MonoHttpConnect.lambda$subscribe$0(HttpClientConnect.java:319)
at reactor.core.publisher.MonoCreate.subscribe(MonoCreate.java:54)
at reactor.core.publisher.Mono.subscribe(Mono.java:3088)
at reactor.core.publisher.FluxRetryPredicate$RetryPredicateSubscriber.resubscribe(FluxRetryPredicate.java:123)
at reactor.core.publisher.MonoRetryPredicate.subscribe(MonoRetryPredicate.java:51)
at reactor.netty.http.client.HttpClientConnect$MonoHttpConnect.subscribe(HttpClientConnect.java:322)
at reactor.core.publisher.MonoFlatMapMany.subscribe(MonoFlatMapMany.java:49)
at reactor.core.publisher.MonoSingle.subscribe(MonoSingle.java:58)
at reactor.core.publisher.MonoFlatMap.subscribe(MonoFlatMap.java:60)
at reactor.core.publisher.MonoMapFuseable.subscribe(MonoMapFuseable.java:59)
at reactor.core.publisher.MonoOnErrorResume.subscribe(MonoOnErrorResume.java:44)
at reactor.core.publisher.FluxSourceMono.subscribe(FluxSourceMono.java:46)
at reactor.core.publisher.MonoSingle.subscribe(MonoSingle.java:58)
at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:53)
at reactor.core.publisher.Mono.subscribe(Mono.java:3088)
at reactor.core.publisher.FluxRetryWhen.subscribe(FluxRetryWhen.java:82)
at reactor.core.publisher.MonoRetryWhen.subscribe(MonoRetryWhen.java:50)
at reactor.core.publisher.FluxSourceMono.subscribe(FluxSourceMono.java:46)
at reactor.core.publisher.FluxOnErrorResume.subscribe(FluxOnErrorResume.java:47)
at reactor.core.publisher.FluxMap.subscribe(FluxMap.java:62)
at reactor.core.publisher.FluxPeek.subscribe(FluxPeek.java:83)
at reactor.core.publisher.FluxMap.subscribe(FluxMap.java:62)
at reactor.core.publisher.FluxPeek.subscribe(FluxPeek.java:83)
at reactor.core.publisher.FluxDefer.subscribe(FluxDefer.java:55)
at reactor.core.publisher.FluxPeek.subscribe(FluxPeek.java:83)
at reactor.core.publisher.MonoSingle.subscribe(MonoSingle.java:58)
at reactor.core.publisher.MonoOnErrorResume.subscribe(MonoOnErrorResume.java:44)
at reactor.core.publisher.MonoFlatMap.subscribe(MonoFlatMap.java:60)
at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:150)
at reactor.core.publisher.MonoDelay$MonoDelayRunnable.run(MonoDelay.java:117)
at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:50)
at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:27)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
2020-02-14 12:20:46.801 WARN 22777 --- [scoveryClient-1] c.netflix.discovery.TimedSupervisorTask : task supervisor timed out
java.util.concurrent.TimeoutException: null
This looks like a reactor-core incorrect version issue.
This dependency might be bringing an older version of reactor-core.
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-cosmosdb-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
Two ways to solve this issue:
Try excluding reactor-core from this dependency.
Try adding reactor-core dependency - 3.3.0 or higher.