Spring boot : 从 @Scheduled 方法访问 rest api
Spring boot : access rest api from @Scheduled method
我尝试了下面的 schedule-task 公会,它成功了。
http://spring.io/guides/gs/scheduling-tasks/
接下来,我想从 @Scheduled
注释方法访问 web-api,并将以下依赖项添加到我的 build.gradle,以使用 RestTemplate
.
compile 'org.springframework.boot:spring-boot-starter-web:1.2.1.RELEASE'
但是,出现以下异常。
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)
原因:org.springframework.context.ApplicationContextException:由于缺少 EmbeddedServletContainerFactory bean,无法启动 EmbeddedWebApplicationContext。
在 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:183)
在 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:156)
在 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)
我需要特定设置才能使用 @Scheduled
方法中的 RestTemplate
吗?
我的全build.gradle如下,其他文件同上。
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'
}
对 spring-boot-starter-web
的依赖适用于需要 servlet 容器的 Web 应用程序。如果您只需要 RestTemplate
,那就太过分了。将依赖替换为 org.springframework:spring-web
我尝试了下面的 schedule-task 公会,它成功了。
http://spring.io/guides/gs/scheduling-tasks/
接下来,我想从 @Scheduled
注释方法访问 web-api,并将以下依赖项添加到我的 build.gradle,以使用 RestTemplate
.
compile 'org.springframework.boot:spring-boot-starter-web:1.2.1.RELEASE'
但是,出现以下异常。
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)
原因:org.springframework.context.ApplicationContextException:由于缺少 EmbeddedServletContainerFactory bean,无法启动 EmbeddedWebApplicationContext。 在 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:183) 在 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:156) 在 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)
我需要特定设置才能使用 @Scheduled
方法中的 RestTemplate
吗?
我的全build.gradle如下,其他文件同上。
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'
}
对 spring-boot-starter-web
的依赖适用于需要 servlet 容器的 Web 应用程序。如果您只需要 RestTemplate
,那就太过分了。将依赖替换为 org.springframework:spring-web