I am having trouble with a Spring Boot Jersey application when running it with java -jar command. When I run the main class from IntelliJ it works just fine. I therefor assumed this could be a classpath issue, but I have used the maven-assembly-plugin, and all the Spring-classes seems to be there.
I have the: spring-boot-starter-web, spring-boot-jersey, spring-boot-starter-tomcat on classpath and for the rest of Spring boot I have included this:
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.1.8.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
My assembly plugin configuration is as follows:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<!-- nothing here -->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>org.online.auth.conf.ApplicationMain</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
When I extract the jar file I find that all Spring dependencies are there (EmbeddedServletContainer and it's subclasses (Tomcat/Jetty)).
The start-up log displays the following error message:
:: Spring Boot ::
2014-11-03 12:46:38.394 INFO 1264 --- [ main] org.online.auth.conf.ApplicationMain : Starting ApplicationMain on R9013DA2 with PID 1264 (C:\projects\online\online-authentication-rest\target\online-authentication-rest-0.0.1-SNAPSHOT-jar-with-dependencies.jar started by thomasa in C:\projects\online\online-authentication-rest\target)
2014-11-03 12:46:38.396 DEBUG 1264 --- [ main] o.s.boot.SpringApplication : Loading source class com.online.auth.conf.ApplicationMain
2014-11-03 12:46:38.412 DEBUG 1264 --- [ main] o.s.b.c.c.ConfigFileApplicationListener : Skipped config file 'file:./config/application.yml' resource not found
2014-11-03 12:46:38.416 DEBUG 1264 --- [ main] o.s.b.c.c.ConfigFileApplicationListener : Loaded config file 'classpath:/application.properties'
2014-11-03 13:36:23.613 INFO 9732 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@43738a82: startup date [Mon Nov 03 13:36:23 CET 2014]; root of context hierarchy
2014-11-03 13:36:23.614 DEBUG 9732 --- [ main] ationConfigEmbeddedWebApplicationContext : Bean factory for org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@43738a82: org.springframework.beans.factory.support.DefaultListableBeanFactory@25b485ba:
defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework .context.annotation.internalAutowiredAnnotationProcessor,
org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.conte xt.annotation.internalCommonAnnotationProcessor,applicationMain]; root of factory hierarchy
2014-11-03 13:36:23.787 INFO 9732 --- [ main] a.ConfigurationClassBeanDefinitionReader : Skipping bean definition for [BeanMethod:name=basicHttpCredentialsUtil,declaringClass=no.safetel.online.auth.AppConfigSpringSecur ity]: a definition for bean 'basicHttpCredentialsUtil' already exists.
This top-level bean definition is considered as an override.
2014-11-03 13:36:23.873 INFO 9732 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2014-11-03 13:36:23.888 DEBUG 9732 --- [ main] ationConfigEmbeddedWebApplicationContext : Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@2dde1bff]
2014-11-03 13:36:23.888 DEBUG 9732 --- [ main] ationConfigEmbeddedWebApplicationContext : Using ApplicationEventMulticaster [org.springframework.context.event.SimpleApplicationEventMulticaster@15bbf42f]
2014-11-03 13:36:23.894 WARN 9732 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:124)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:476)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
I have tried to explicitly define an EmbeddedServletContainerFactory but that just gave me another exception with a message saying A ServletContext is required to configure default servlet handling.
Any help on this issue would be much appreciated!
Edit!
I tried to print every bean that is created by the container by implementing BeanPostProcessor:
@ComponentScan
@EnableAutoConfiguration(exclude = {
DataSourceAutoConfiguration.class,
DataSourceInitializer.class,
DataSourceTransactionManagerAutoConfiguration.class,
SecurityAutoConfiguration.class})
public class ApplicationMain extends SpringBootServletInitializer implements BeanPostProcessor {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(ApplicationMain.class, args);
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("\t\tbean: "+ beanName);
return bean;
}
}
Running this in IntelliJ gives me the following output:
2014-11-03 16:11:26.348 INFO 13548 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'metaDataSourceAdvisor' of type [class org.springframework.security.access.intercept.aopalliance.MethodSecurityMetadataSourceAdvisor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
bean: org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat
bean: tomcatEmbeddedServletContainerFactory
bean: org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration
bean: org.hibernate.validator.internal.constraintvalidators.NotNullValidator
bean: serverProperties
bean: org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration
2014-11-03 16:11:26.889 INFO 13548 --- [ main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080
Running the jar file with the java command gives me the following output:
2014-11-03 16:01:36.302 INFO 6704 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'metaDataSourceAdvisor' of type [class org.springframework.security.access.intercept.aopalliance.MethodSecurityMetadataSourceAdvisor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2014-11-03 16:01:36.310 WARN 6704 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:124)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:476)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
Any help would be much appreciated!
I finally figured it out after reading the documentation more thoroughly. The Spring docs clearly states that you need to use the spring-boot-maven-plugin in the build section of your pom.xml.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
I have also added a property which declares the main-class:
<properties>
<start-class>org.online.auth.ApplicationMain</start-class>
</properties>
Hope this can be of help for others.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With