Spring Boot:从@Scheduled方法访问rest api

吉田由纪

我尝试下面的计划任务的公会,它的工作。

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(EmbeddedWebApplicationorg.boot。 .embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)

我需要特定的设置来使用RestTemplatefrom@Scheduled方法吗?

我的完整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'
}
安迪·威尔金森(Andy Wilkinson)

对的依赖spring-boot-starter-web旨在用于需要servlet容器的Web应用程序。如果您需要的只是这太过分了RestTemplate将依赖项替换为onorg.springframework:spring-web

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章