java.lang.NoClassDefFoundError when running KafkaEmbedded for Unit test - spring

I have a Spring-Kafka project running Spring Boot 2.0.2. In the unit test I am trying to start a KafkaEmbedded server. I get this error.
java.lang.NoClassDefFoundError: org/apache/kafka/common/security/auth/SecurityProtocol
at kafka.utils.TestUtils$.createBrokerConfig(TestUtils.scala:222)
at kafka.utils.TestUtils.createBrokerConfig(TestUtils.scala)
at org.springframework.kafka.test.rule.KafkaEmbedded.createBrokerProperties(KafkaEmbedded.java:278)
at org.springframework.kafka.test.rule.KafkaEmbedded.before(KafkaEmbedded.java:224)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:46)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: org.apache.kafka.common.security.auth.SecurityProtocol
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 13 more
I am aware that this is because of the dependencies mis-match. But I have added the latest versions spring-kafka-test & kafka-clients. I am aware that this class "SecurityProtocol" is in the Kafka-clients jar but adding that dependency (old & new versions) still does not solve the problem.
The following is the dependencies in pom file.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<version>2.1.6.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
Here is the basic Unit test that uses the EmbeddedKafka.
#RunWith(SpringRunner.class)
#SpringBootTest
public class KafkaDemoApplicationTests {
#ClassRule
public static KafkaEmbedded embeddedKafka = new KafkaEmbedded(1, true,
Topology.DEMO_TOPIC);
#Test
public void contextLoads() {
}
}

You have the wrong kafka (scala) jars on the classpath.
java.lang.NoClassDefFoundError: org/apache/kafka/common/security/auth/SecurityProtocol
SecurityProtocol was moved from org.apache.kafka.common.protocol to org.apache.kafka.common.security.auth in kafka-clients 1.0.0.
So it looks like you have (somehow) got the older kafka_2.11 jar on the CP; spring-kafka-test should bring in the right version. Perhaps you have overrides in your pom that you are not showing?

The solution was to avoid specifying versions for all Kafka related dependencies in the Spring Boot project's pom.xml file. Like this -
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>

If you are using kafka-client (not spring kafka) , make sure the kafka-client version used in your pom, is the same version as used in the spring-kafka test dependency.
Here in the spring-kafka-test maven repo [https://mvnrepository.com/artifact/org.springframework.kafka/spring-kafka-test], it lists all the dependencies, including the kafka-client it uses.
So either update you kafka-client version or the spring-kafka-test version
to make sure clients are the same.

Related

org/springframework/boot/bind/RelaxedPropertyResolver not found

I am trying to connect Apache Artemis with Apache Camel in Spring Boot project.
I am getting problem when I am upgrading my spring boot version from 1.5.9.RELEASE to 2.2.2.RELEASE. Below is my POM and Java files which I am using:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.learncamel</groupId>
<artifactId>activemq-learncamel-spring-boot</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>learncamel-spring-boot</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<camel-version>2.20.1</camel-version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>2.20.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>1.5.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jdbc</artifactId>
<version>${camel-version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1212</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
<!-- EMail-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
<!--activeMQ-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-camel</artifactId>
<version>5.15.3</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jms</artifactId>
<version>2.20.1</version>
</dependency>
<!--health Check-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-http</artifactId>
<version>2.20.1</version>
</dependency>
<!--Test Dependencies-->
<!--Camel Spring Test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-spring</artifactId>
<version>2.20.1</version>
<scope>test</scope>
</dependency>
<!--Clean Up directory-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
When I am upgrading parent version to 2.2.2.RELEASE then it start giving below error:-
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
21-06-2020 15:39:11.614 [main] ERROR o.s.boot.SpringApplication.reportFailure - Application run failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.learncamel.ActiveMQCamelApplication]; nested exception is java.lang.IllegalStateException: Could not evaluate condition on org.apache.camel.spring.boot.security.CamelSSLAutoConfiguration due to org/springframework/boot/bind/RelaxedPropertyResolver not found. Make sure your own configuration does not rely on that class. This can also happen if you are #ComponentScanning a springframework package (e.g. if you put a #ComponentScan in the default package by mistake)
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:597)
at org.springframework.context.annotation.ConfigurationClassParser.access$900(ConfigurationClassParser.java:109)
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGroupingHandler.lambda$processGroupImports$1(ConfigurationClassParser.java:805)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGroupingHandler.processGroupImports(ConfigurationClassParser.java:801)
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorHandler.process(ConfigurationClassParser.java:771)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:188)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:325)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:242)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:706)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
at com.learncamel.ActiveMQCamelApplication.main(ActiveMQCamelApplication.java:10)
Caused by: java.lang.IllegalStateException: Could not evaluate condition on org.apache.camel.spring.boot.security.CamelSSLAutoConfiguration due to org/springframework/boot/bind/RelaxedPropertyResolver not found. Make sure your own configuration does not rely on that class. This can also happen if you are #ComponentScanning a springframework package (e.g. if you put a #ComponentScan in the default package by mistake)
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:54)
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:108)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:221)
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:587)
... 19 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/springframework/boot/bind/RelaxedPropertyResolver
at org.apache.camel.spring.boot.security.CamelSSLAutoConfiguration$Condition.getMatchOutcome(CamelSSLAutoConfiguration.java:51)
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47)
... 22 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.bind.RelaxedPropertyResolver
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 24 common frames omitted
Process finished with exit code 1
I am using below Java files:
package com.learncamel.config;
import org.apache.activemq.camel.component.ActiveMQComponent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.jms.ConnectionFactory;
#Configuration
public class ActiveMQConfig {
#Bean(name="activemq")
public ActiveMQComponent createComponent(ConnectionFactory factory){
ActiveMQComponent activeMQComponent = new ActiveMQComponent();
activeMQComponent.setConnectionFactory(factory);
return activeMQComponent;
}
}
Another Java file:
package com.learncamel.routes;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
#Component
public class ActiveMQRoute extends RouteBuilder{
#Override
public void configure() throws Exception {
from("{{fromRoute}}")
.log("Read Message from activemQ ${body}")
.to("{{toRoute}}");
}
}
Such errors could arise because of mixed Spring versions.
Do a Maven dependency tree and check for mixed Spring versions (the one of the parent POM and other versions from transitive dependencies.
If you got mixed Spring versions in your dependency tree, fix them with dependency management or similar Maven features.
I had spring-boot-starter-parent version in pom as 2.3.10.RELEASE but added spring-boot dependency of 2.1.8.RELEASE. I had to remove version from there to make the error go away
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<!--<version>2.1.8.RELEASE</version>-->
</dependency>

Cannot load driver class: org.h2.Driver in spring boot application

I am trying to learn microservices by creating a dummy project.
I have a config repository in git and a config server running at port 8888.It's working properly as I can see my settings as below:
Now I have two microservice projects 1) customer service and 2) customer-account-service. customer microservice is running properly while customer-account-microservice is unable to start and throwing below exception:
*Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: org.h2.Driver
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.0.11.RELEASE.jar:5.0.11.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:583) ~[spring-beans-5.0.11.RELEASE.jar:5.0.11.RELEASE]
... 66 common frames omitted
Caused by: java.lang.IllegalStateException: Cannot load driver class: org.h2.Driver
at org.springframework.util.Assert.state(Assert.java:94) ~[spring-core-5.0.11.RELEASE.jar:5.0.11.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:224) ~[spring-boot-autoconfigure-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:176) ~[spring-boot-autoconfigure-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:43) ~[spring-boot-autoconfigure-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari.dataSource(DataSourceConfiguration.java:83) ~[spring-boot-autoconfigure-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_144]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.0.11.RELEASE.jar:5.0.11.RELEASE]
... 67 common frames omitted*
bootstrap.properties configuration for customer-account-microservice is as below:
pom.xml configuration for customer-account-microservice is as below:
<?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 http://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.0.7.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.microservice</groupId>
<artifactId>customer-account-microservice</artifactId>
<version>1</version>
<name>customer-account-microservice</name>
<description>Spring starter project for demonstrating spring feign client and circuit breaker</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud-services.version>2.0.3.RELEASE</spring-cloud-services.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.pivotal.spring.cloud</groupId>
<artifactId>spring-cloud-services-starter-circuit-breaker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</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>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.pivotal.spring.cloud</groupId>
<artifactId>spring-cloud-services-dependencies</artifactId>
<version>${spring-cloud-services.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Please help me in fixing the above exception.
It seems that your dependency
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
has to be in dependencies element, not in dependencyManagement. Just look at this. Or maybe you have a child artifact, in that case you must share another pom xml.
I change it to 'compile' and it solved my problem.
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<scope>compile</scope>
</dependency>
in my case, I had a space after the "driver"
spring.datasource.driverClassName=org.h2.Driver...
I deleted it and it works just fine
I was facing the same issue in IDEA IntellJ even after using the correct dependency in pom.xml. It boils down to IDE specific issue where it was not able to download h2 dependency. It worked after restarting the IDE.
In my case the problem was caused by some trailing blank characters behind the driver classname in application.properties
This fails (dots in the code snippet below should be read as whitespace chars):
spring.datasource.driverClassName=org.h2.Driver...
This succeeds:
spring.datasource.driverClassName=org.h2.Driver
I commented spring.datasource.driverClassName=org.h2.Driver, that worked like a charm for me
In my case I gave the gradle dependency wrong.
I should give it in dependencies {implementation}, but I gave it in dependencies {testImplementation} in build.gradle file
The below mentioned is right
dependencies {
implementation 'com.h2database:h2:2.1.212'
}
I had this problem because I was using an outdated version of the h2 dependency :
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.193</version>
</dependency>
I fixed it by removing the version tag so that the latest version is downloaded :
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
I hope you have added dependencies now add spring.datasource.platform=h2 in your application.properties
it worked for me
In my case,
In application.properties file, there are white spaces after adding configurations
You can see IDEA IntellJ like this
Should remove all the white spaces in application.properties file. Then the test cases failed issue will be solved

Spring Boot App module with custom parent - getting it to work

The goal: getting the Spring Boot app module which works perfectly with the spring-boot-starter-parent, to work with our own custom parent pom.
I am following this guide and am sorry to say I cannot get this to fully work.
https://www.surasint.com/spring-boot-with-no-parent-example/.
Concretely, with having the spring-boot-starter as parent pom everything works fine.
With our own parent pom, the Spring Boot app breaks.
I have a project setup with three modules and my own parent pom. This is needed for many reasons (the parent pom uses Maven profiles and sets important database variables depending on these profiles).
Parent pom (our custom).
Common – simple jar library.
Web – Spring Boot app with REST services and pure Java helpers (NOT any jsp or html). This used to inherit from spring-boot-starter parent – and now must inherit from our custom parent pom.
Worker – simple jar library with a worker thread.
I am using Spring Boot 1.5.9, Maven 3.3 and Java 8.
In theory I totally agree that it should work without having the spring-boot-starter-parent. However when making the Spring Boot module inheriting only from our parent pom and NOT from the spring-boot-starter-parent, the following (to me very weird) exceptions occur.
2018-02-07 16:06:46.822 ERROR 10008 --- [ main]
o.s.boot.SpringApplication : Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Factory method 'requestMappingHandlerAdapter' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcValidator' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.validation.Validator]: Factory method 'mvcValidator' threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration.getApplicationContext()Lorg/springframework/context/ApplicationContext;
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
The project is fully green in IntelliJ and compile is OK. It is only when starting the Spring Boot app that these errors occurs. As I interpret it the Spring boot app module is now lacking something which it had before. Any advice here would be most helpful!
Concretely, what do you need to import (in Maven dependency-tags) for the module to have access to exactly the same thing – and everything – that it had when it was inheriting from the spring-boot-starter-parent instead of your own custom parent? It would be super that instead of by pure intuition guessing your dependencies (which apparently fails in my situation) there is a reference dependency checklist that makes sure your module have access to exactly the same thing as if inheriting from the spring-boot-starter-parent directly.
So far I've by pure intuition added the following to the Spring boot module (called "web" above)
<!-- === [Spring Boot Starter] === -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Evidently something is missing which was present with the spring-boot-starter as parent. Moreover - and oddly enough - the rest of the possible spring-boot-starter dependencies seem totally irrelevant for the project (things like cloud-connectors, twitter-connectors, mongodb and related). I added all remotely relevant just to test but the errors below are still there. Any advice or help is most appreciated.
As a reference here is the parent pom:
<?xml version="1.0" encoding="UTF-8"?>
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
no.statnett.fasit
fasit-hub-backend
1.0-SNAPSHOT
common
worker
web
pom
Fasit Hub Backend
<!-- parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.source.encoding>UTF-8</project.source.encoding>
<project.source.version>1.8</project.source.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.boot.version>1.5.9.RELEASE</spring.boot.version>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencyManagement>
<dependencies>
<!--
Import dependency management from Spring Boot
* This fixes the Spring boot parent issue [inherit dependencyManagement without forcing Spring Boot App]
* It does NOT include the plugin management
See https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-build-systems.html#using-boot-maven-without-a-parent
See http://www.logicbig.com/tutorials/spring-framework/spring-boot/starter-import/
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<!-- === [Spring Boot JPA] ===-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.196</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.1</version>
</dependency>
<!-- === [Spring Boot Security] ===-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<!-- === [Spring Boot Maven plugin needed for profile management] ===-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<!-- === [Messaging] ===-->
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
</dependency>
-->
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<!-- ===[Messaging with AMQP over JMS with the Apache QPID client] === -->
<dependency>
<groupId>org.apache.qpid</groupId>
<artifactId>qpid-jms-client</artifactId>
<version>0.24.0</version>
</dependency>
<!-- === [Date and Time management] ===-->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9.4</version>
</dependency>
<!-- === [XStream for converting classes to and from XML] === -->
<!-- NB!
XStream also needs Apache's commons-io
2017.10.15: The artifact org.apache.commons:commons-io:jar:1.3.2 has been relocated to commons-io:commons-io:jar:1.3.2
See https://mvnrepository.com/artifact/org.apache.commons/commons-io
See https://mvnrepository.com/artifact/commons-io/commons-io
-->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.10</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<!-- === [Spring MVC and WEB (javax.servlet, JSP/JSTL] === -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- === [Logging and Testing] === -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</dependencyManagement>
<profiles>
<profile>
<id>dev</id>
<properties>
<activatedProperties>dev</activatedProperties>
<datasourceURL>jdbc:oracle:thin:#h1-a-oradb10t:1521:fasitngft1</datasourceURL>
<datasourceUser>fasithub</datasourceUser>
<datasourcePassword>cH9nIiBzuS5Ho3q1bOP5</datasourcePassword>
<datasourceDriver>oracle.jdbc.OracleDriver</datasourceDriver>
<ecpServer>amqp://ec2-52-34-81-131.us-west-2.compute.amazonaws.com:5672</ecpServer>
<ecpServerSolnett>amqp://ec2-52-40-134-91.us-west-2.compute.amazonaws.com:5672</ecpServerSolnett>
<ecpServerVindnett>amqp://ec2-34-213-80-66.us-west-2.compute.amazonaws.com:5672</ecpServerVindnett>
<ecpEndpointSuffix></ecpEndpointSuffix>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<activatedProperties>test</activatedProperties>
<datasourceURL>jdbc:h2:file:~/test</datasourceURL>
<datasourceUser>sa</datasourceUser>
<datasourcePassword></datasourcePassword>
<datasourceDriver>org.h2.Driver</datasourceDriver>
<ecpServer>amqp://ec2-52-34-81-131.us-west-2.compute.amazonaws.com:5672</ecpServer>
<ecpServerSolnett>amqp://ec2-52-40-134-91.us-west-2.compute.amazonaws.com:5672</ecpServerSolnett>
<ecpServerVindnett>amqp://ec2-34-213-80-66.us-west-2.compute.amazonaws.com:5672</ecpServerVindnett>
<ecpEndpointSuffix></ecpEndpointSuffix>
</properties>
</profile>
<profile>
<id>staging</id>
<properties>
<activatedProperties>staging</activatedProperties>
<datasourceURL>jdbc:oracle:thin:#h1-a-oradb10t:1521:fasitngft1</datasourceURL>
<datasourceUser>fasithub</datasourceUser>
<datasourcePassword>cH9nIiBzuS5Ho3q1bOP5</datasourcePassword>
<datasourceDriver>oracle.jdbc.OracleDriver</datasourceDriver>
<ecpServer>amqp://h1-a-ecp4end:6672</ecpServer>
<ecpServerSolnett>amqp://ec2-52-40-134-91.us-west-2.compute.amazonaws.com:5672</ecpServerSolnett>
<ecpServerVindnett>amqp://ec2-34-213-80-66.us-west-2.compute.amazonaws.com:5672</ecpServerVindnett>
<ecpEndpointSuffix>FASIT-SERVICE</ecpEndpointSuffix>
</properties>
</profile>
</profiles>
And here is the spring boot app module pom (here is where current the trial-and-error occurrs):
<?xml version="1.0" encoding="UTF-8"?>
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<!-- Alt.1: have our own parent: this is the way to go for a common jar module.
ISSUE: Spring Boot module fails to run - applicationContext fails to load!
Solution: explicitly define goal for spring-boot-maven-plugin (If you use Spring Boot as parent, you do not have to explicitly define this)
-->
<parent>
<groupId>no.statnett.fasit</groupId>
<artifactId>fasit-hub-backend</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<!-- Alt.2: have spring-boot-starter-parent as parent. This is useful for a Spring Boot app module.
ISSUE: profile management breaks - properties are not set on the Spring Boot module!
Solution: not possible without our own parent pom
-->
<!-- parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/>
</parent -->
<!-- Spring Boot starter properties overrule (default Java is only 1.6) -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<!-- spring.boot.version>1.5.6.RELEASE</spring.boot.version -->
</properties>
<groupId>no.statnett.fasit</groupId>
<artifactId>web</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<!-- === [Dependency Management] === -->
<dependencies>
<dependency>
<groupId>no.statnett.fasit</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
<!-- === [Spring Boot Starter] === -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</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-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- Following three dependencies where not needed with spring-boot-starter-parent as parent:
spring-boot-starter-tomcat, spring-boot-starter-thymeleaf, spring-boot-starter-data-rest
STILL GET ISSUE:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate
[org.springframework.data.rest.core.config.RepositoryRestConfiguration]:
Factory method 'config' threw exception; nested exception is java.lang.NoClassDefFoundError:
org/springframework/data/rest/core/config/RepositoryCorsRegistry
-->
<!-- === [Spring Boot JPA] === -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<exclusions>
<exclusion>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- === [Oracle driver official, manually installed] ===-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3.0</version>
<type>pom</type>
</dependency>
<!-- === [Spring Boot Security] ===-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- === [Date and Time management] ===-->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
</dependency>
<!-- === [XStream for converting classes to and from XML] === -->
<!-- NB! moved to the common module, they are not needed here in the web service module! -->
</dependencies>
<!-- === [Build management for the Spring Boot module] === -->
<build>
<plugins>
<!-- === [Spring Boot Maven plugin is used for profile management] ===-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- === [Apache Maven compiler plugin for building] ===-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
UPADTE 1:
if I add the following three dependencies to the spring app module pom:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
I instead get the following excetpion:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.rest.core.config.RepositoryRestConfiguration]: Factory method 'config' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/data/rest/core/config/RepositoryCorsRegistry
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
... 144 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/springframework/data/rest/core/config/RepositoryCorsRegistry
I am not sure how to interpret this. Does this mean we are on the right track and just need to add some additional depedency, OR where the dependencies added needlessly and perhaps causing unnessecary problems?
The missing class now is https://docs.spring.io/spring-data/rest/docs/current/api/org/springframework/data/rest/core/config/RepositoryCorsRegistry.html
I am not sure exactly what to add as dependency to get access to this class in the right way with Spring Boot. Naturally I am reluctant to add it through a normal spring framework dependency like https://mvnrepository.com/artifact/org.springframework/spring-web/5.0.3.RELEASE since that often causes version problems (Spring Boot includes the relevant springframeworkd and should be managed totally through Spring Boot as I understand it and been told many times).
UPDATE 2:
Perhaps against better judgement – and since I'm willing to test anything until things are working – I add the following libraries which have the currently missing class.
This is of course only done after carefully checking https://mvnrepository.com/artifact/org.springframework.boot/spring-boot/1.5.9.RELEASE for exactly which core org.springframework library are used by the Spring Boot version.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
I now get a different exception:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.filter.OrderedHttpPutFormContentFilter]: Factory method 'httpPutFormContentFilter' threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.fasterxml.jackson.databind.ObjectMapper
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
... 26 common frames omitted
Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.fasterxml.jackson.databind.ObjectMapper
at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.build(Jackson2ObjectMapperBuilder.java:588) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
Which leads me to add the following dependency (again after carefully checking the Spring Boot reference above to see what version of the specific library it uses):
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.4</version>
</dependency>
However, sadly and oddly enough – the same exception is still there. This means we have hit a dead end since there are no more logical libraries to include from https://mvnrepository.com/artifact/org.springframework.boot/spring-boot/1.5.9.RELEASE.
And regardless, it is not best practice to include specific core springframework library versions when working with a Spring Boot app.
So let's take a step back and look at what we need.
The Spring Boot app module contains a set of REST services.
One set of the services are only GET requests, and produces application/xml
(for integration with systems of type alpha)
Another set of the
services produces and consumes application/json (for integration with
systems of type omega).
That's it. All core service and JPA logic is in the common library module which is needed by both the web Spring Boot app module, and the worker module.
Bottom line: what spring-boot dependencies do we need to import to get this to work? Again it works perfectly fine when having the spring-boot-starter-parent as pom parent – but that is not possible in the given situation. Suffice to say it would be super to finally get this working
Cheers!
Is there some reason that the Spring Boot Starter Parent cannot be the parent of your parent pom? I see that it is commented out in your custom parent.
This setup should work:
Custom Parent pom
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.16.RELEASE</version>
</parent>
<!-- All of your customizations follow -->
<dependencies>
<!-- Define common dependencies for all child modules-->
</dependencies>
<dependencyManagement>
<!-- Define optional module dependencies with preferred versions -->
<!-- This can help the module developers to not have to deal with dependency conflicts if you handle them here -->
</dependencyManagement>
<build>
<plugins>
<!-- Define common plugin configs for all child modules -->
</plugins>
<pluginManagement>
<plugins>
<!-- Define optional plugin configs for child modules -->
</plugins>
</pluginManagement>
</build>
App pom
<parent>
<groupId>no.statnett.fasit</groupId>
<artifactId>fasit-hub-backend</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
Honestly, that should be it. You've then created a custom parent pom that inherits everything correctly from the Spring Boot Starter Parent, and you can provide all of your own profiles and other customization that you want your App module(s) to inherit.
You should not have to go through all of the trouble of recreating the dependency tree of a Spring Boot Application, and as you've noted, doing so would basically be a hellscape of dependency management.
I have now finally managed to solve this issue by brute force trial-and-error testing.
Suffice to say there are a huge number of possible of combinations of dependencies to test. As a developer this is by far among the most tedious and time-wasting task you can perform.
I am not flagging this as an acceptable answer simply because I am not happy at all with it.
Suffice to say, it would really be super with a reference set for dependency inclusions so you get exactly what you get as with the spring-boot-starter-parent. Also it does not feel like best practice to include specific core org.springframework dependencies when you are working with Spring Boot. If you are not super disciplined and doublecheck the exact library versions used by your specific Spring Boot version you will run into problems.
Best practice - as I understand it and also been told many times - is that you only import pure Spring Boot dependencies, and Spring Boot manages the rest. Evidently that sadly and suprisingly enough is not the case. If anyone has proof of the contrary, I'm all ears.
In summary these are the dependencies that solved the spring boot app module containing REST services (with both application/json and application/xml and automatic object mapping).
<!-- === [Spring Boot Starter] === -->
<!-- Compile -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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-web</artifactId>
</dependency>
<!-- NB!
spring-boot-starter-web also needs the following [Spring Boot app fails to start without them]
These dependencies where NOT needed with spring-boot-starter-parent as parent.
Without them you e.g. get the following on Spring Boot app start:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate
[org.springframework.data.rest.core.config.RepositoryRestConfiguration]:
Factory method 'config' threw exception; nested exception is java.lang.NoClassDefFoundError:
org/springframework/data/rest/core/config/RepositoryCorsRegistry
-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.10</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.3.6.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<!-- Provided -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

Spring boot making deployable WAR file

I am using Spring boot and Spring JPA Data to build a web application.
The application works fine, but when I try to make WAR file to deploy it inside Tomcat 8 web server, I get these errors
Here is my Spring boot start class
#SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder
application) {
return application.sources(SpringBootWebApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
}
}
and here is my POM file :
<?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
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>guru.springframework</groupId>
<artifactId>spring-boot-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Spring Boot Web Application</name>
<description>Spring Boot Web Application</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<!--<start-class>guru.springframework.SpringBootWebApplication </start-class>-->
</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-security</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-thymeleaf</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--WebJars-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.4</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
<version>LATEST</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-ldap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-openid</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- hot swapping, disable cache for template, enable live reload -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<finalName>Mobl</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.1.RELEASE</version>
</plugin>
</plugins>
</build>
and IntelliJ Idea gives me these Errors :
Error:java: com.sun.tools.javac.code.Symbol$CompletionFailure: class file
for groovy.lang.Closure not found
Error:java: java.lang.RuntimeException:
com.sun.tools.javac.code.Symbol$CompletionFailure: class file for
groovy.lang.Closure not found
Spring Boot already contains a Tomcat container inside it, so your generated JAR file is executable.
Thankfully, Spring authors thought about some ppl still want to deploy Spring Boot apps into an individual container. Please follow this link for further info.
You need to configure the Maven/Gradle plugin to generate WAR instead of a JAR.
Edit: I just saw that you already have the configuration. If your application is working fine outside of IntelliJ then the issue might be that IntelliJ recognizes your application as a Spring Boot app and tries to run the JAR instead of deploying the WAR file as I think IntelliJ is not prepared for this case. You might have to write a custom configuration for it.

LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation

I try to start my spring boot in tomcat server but when I'm adding some external jars to my project :
appsert-rt.jar
gf-client.jar
javaee.jar
it gives me that error :
Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.slf4j.impl.JDK14LoggerFactory loaded from file:/C:/Users/amar/Documents/glassfish-3.1/glassfish/modules/bean-validator.jar). If you are using Weblogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml Object of class [org.slf4j.impl.JDK14LoggerFactory] must be an instance of class ch.qos.logback.classic.LoggerContext
at org.springframework.util.Assert.isInstanceOf(Assert.java:339)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:151)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLogger(LogbackLoggingSystem.java:143)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:89)
at org.springframework.boot.logging.LoggingApplicationListener.onApplicationStartedEvent(LoggingApplicationListener.java:132)
at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:122)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:151)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:128)
at org.springframework.boot.context.event.EventPublishingRunListener.publishEvent(EventPublishingRunListener.java:100)
at org.springframework.boot.context.event.EventPublishingRunListener.started(EventPublishingRunListener.java:54)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:278)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:961)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:950)
at demo.DemoApplication.main(DemoApplication.java:14)
My pom :
<?xml version="1.0" encoding="UTF-8"?>
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.udev</groupId>
<artifactId>udev</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>demo.DemoApplication</start-class>
<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-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.6.4.RELEASE</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.14.8</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
note : it work perfectly when i remove the jars / One problem could be conflits between classes of slf4j in my maven added jars and those added externelly
There is a conflict between the jars declared in your maven project and those you imported explicitly.
Dependency analysis is one of the great benefits of using a tool like Maven, but it cannot do anything about dependencies that you are manually adding a runtime.
What you should do is add those jars as dependencies to your maven project. Once you do that you can use the Maven Dependency Plugin to find the conflicting packages, and add exclusions for packages that conflict.

Resources