Spring boot : access rest api from @Scheduled method Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Spring boot : access rest api from @Scheduled method without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I try below guild of schedule-task and it worked.

http://spring.io/guides/gs/scheduling-tasks/

next, I want to access web-api from @Scheduled annotated method, and add below dependency to my build.gradle, to use RestTemplate.

compile 'org.springframework.boot:spring-boot-starter-web:1.2.1.RELEASE'

but, below Exception occurs.

Exception in thread "main" 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:133)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:474)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:961)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:950)
at my.pkg.Application.main(BatchApplication.java:14)

Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean. at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:183) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:156) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)

do I need specific setting to use RestTemplate from @Scheduled method ?

my full build.gradle is below, other files are same to guide.

buildscript {
  repositories {
    mavenLocal()
    mavenCentral()
  }
  dependencies {
    classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.2.1.RELEASE'
  }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'

repositories {
  mavenLocal()
  mavenCentral()
}

dependencies {
  compile 'org.springframework.boot:spring-boot-starter:1.2.1.RELEASE'
  // below line I added.
  compile 'org.springframework.boot:spring-boot-starter-web:1.2.1.RELEASE'
}

Answer

A dependency on spring-boot-starter-web is intended for web applications that need a servlet container. It’s overkill if all you need is RestTemplate. Replace the dependency with one on org.springframework:spring-web

We are here to answer your question about Spring boot : access rest api from @Scheduled method - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji