7

Spring boot documentation claims that setting server.port=-1 disables http endpoint, but for me it behaves the same as if I used port=0. What I want to achieve is a batch spring boot application which starts, does some things and shuts down. Right now it just sits there until I kill it.

How to prevent it from starting http server? Or instead - how to shutdown Spring boot application gracefully from the code?

I'm using version 1.2.4.RELEASE. I have following annotations on the main class:

@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableCaching

Edit - my dependencies:

compile(
    'redis.clients:sharded-jedis-sentinel-pool:0.2.10.08e7bbe',
    'org.springframework.boot:spring-boot-starter-jdbc',
    'org.springframework.boot:spring-boot-starter-web',
    'mysql:mysql-connector-java:5.1.33',
    'org.flywaydb:flyway-core:3.2.1',
)

Without spring-boot-starter-web spring throws:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.GzipFilterAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.boot.autoconfigure.web.GzipFilterProperties org.springframework.boot.autoconfigure.web.GzipFilterAutoConfiguration.properties; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.http.gzip.CONFIGURATION_PROPERTIES': Initialization of bean failed; nested exception is java.lang.TypeNotPresentException: Type org.springframework.http.HttpMethod not present
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
    at my.Application.main(Application.java:19)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.boot.autoconfigure.web.GzipFilterProperties org.springframework.boot.autoconfigure.web.GzipFilterAutoConfiguration.properties; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.http.gzip.CONFIGURATION_PROPERTIES': Initialization of bean failed; nested exception is java.lang.TypeNotPresentException: Type org.springframework.http.HttpMethod not present
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 15 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.http.gzip.CONFIGURATION_PROPERTIES': Initialization of bean failed; nested exception is java.lang.TypeNotPresentException: Type org.springframework.http.HttpMethod not present
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 17 more
Caused by: java.lang.TypeNotPresentException: Type org.springframework.http.HttpMethod not present
    at sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:117)
    at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125)
    at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
    at sun.reflect.generics.visitor.Reifier.reifyTypeArguments(Reifier.java:68)
    at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:138)
    at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
    at sun.reflect.generics.repository.ConstructorRepository.getParameterTypes(ConstructorRepository.java:94)
    at java.lang.reflect.Executable.getGenericParameterTypes(Executable.java:279)
    at java.lang.reflect.Method.getGenericParameterTypes(Method.java:268)
    at java.beans.FeatureDescriptor.getParameterTypes(FeatureDescriptor.java:387)
    at java.beans.MethodDescriptor.setMethod(MethodDescriptor.java:116)
    at java.beans.MethodDescriptor.<init>(MethodDescriptor.java:72)
    at java.beans.MethodDescriptor.<init>(MethodDescriptor.java:56)
    at java.beans.Introspector.getTargetMethodInfo(Introspector.java:1163)
    at java.beans.Introspector.getBeanInfo(Introspector.java:426)
    at java.beans.Introspector.getBeanInfo(Introspector.java:173)
    at org.springframework.beans.CachedIntrospectionResults.<init>(CachedIntrospectionResults.java:279)
    at org.springframework.beans.CachedIntrospectionResults.forClass(CachedIntrospectionResults.java:189)
    at org.springframework.beans.BeanWrapperImpl.getCachedIntrospectionResults(BeanWrapperImpl.java:328)
    at org.springframework.beans.BeanWrapperImpl.getPropertyDescriptors(BeanWrapperImpl.java:336)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.filterPropertyDescriptorsForDependencyCheck(AbstractAutowireCapableBeanFactory.java:1364)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.filterPropertyDescriptorsForDependencyCheck(AbstractAutowireCapableBeanFactory.java:1343)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1205)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    ... 26 more
Caused by: java.lang.ClassNotFoundException: org.springframework.http.HttpMethod
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:344)
    at sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:114)
    ... 49 more
mabn
  • 2,473
  • 1
  • 26
  • 47
  • afaik port=0 is random port. – sodik Jun 29 '15 at 16:27
  • Perhaps you have unnecessary libraries on your classpath? Remove the dependencies. – Vaelyr Jun 29 '15 at 17:09
  • 1
    Please show your pom.xml. If you don't want an HTTP server, and you have spring-boot-starter-web in your POM, remove it. – dunni Jun 29 '15 at 17:10
  • I've added list of dependencies. The problem is that when I remove 'org.springframework.boot:spring-boot-starter-web' spring throws: `Caused by: java.lang.ClassNotFoundException: org.springframework.http.HttpMethod` – mabn Jun 29 '15 at 17:41
  • Where do you want to use this? If you really need it, you should include just the dependencies, which bring the classes you really need. spring-boot-starter-web includes an embedded Tomcat and activates the autoconfiguration for it. – dunni Jun 29 '15 at 17:42
  • I just want to run small job which exists after it's done. No http, just redis+database. Unfortunately Spring throws exceptions if there's no spring-boot-starter-web on the classpath. – mabn Jun 29 '15 at 17:44
  • 1
    Do you have Jetty on the classpath? Because the GzipFilterAutoConfiguration is only activated, if it finds Jetty. Maybe the redis library brings Jetty as transitive dependency. – dunni Jun 29 '15 at 17:46
  • I actually did, pulled in by a library I forgot about. But I also found a way to silence it, this did the trick: `@EnableAutoConfiguration(exclude = GzipFilterAutoConfiguration.class)` – mabn Jun 29 '15 at 17:57
  • The problem with `GzipFilterAutoConfiguration` is a bug. Sorry for the trouble, and thank you for bringing it to my attention. I've opened (and fixed) https://github.com/spring-projects/spring-boot/issues/3362. – Andy Wilkinson Jun 30 '15 at 12:09
  • https://stackoverflow.com/questions/26105061/spring-boot-without-the-web-server/44394305#44394305 – kinjelom Feb 14 '20 at 09:38

3 Answers3

22

Since web-environment has been deprecated an may be removed soon, you should use:

spring.main.web-application-type=none
rgrebski
  • 2,354
  • 20
  • 29
8

To disable the web environment, add this line into the application.properties file:

spring.main.web-environment=false
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Remi Venant
  • 153
  • 2
  • 5
0

Add this property inside your application.properties file:-

spring.main.web-application-type=none