简体   繁体   English

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

[英]Spring boot : access rest api from @Scheduled method

I try below guild of schedule-task and it worked. 我尝试下面的计划任务的公会,它的工作。

http://spring.io/guides/gs/scheduling-tasks/ 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 . 接下来,我想从@Scheduled注释方法访问web-api,并将以下依赖项添加到build.gradle中,以使用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. 原因:org.springframework.context.ApplicationContextException:由于缺少EmbeddedServletContainerFactory bean而无法启动EmbeddedWebApplicationContext。 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) 在org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationorg.boot:conmb.framework.boot:context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:183)处。 .embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)

do I need specific setting to use RestTemplate from @Scheduled method ? 我需要特定的设置来使用@Scheduled方法中的RestTemplate吗?

my full build.gradle is below, other files are same to guide. 我的完整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'
}

A dependency on spring-boot-starter-web is intended for web applications that need a servlet container. spring-boot-starter-web的依赖旨在用于需要servlet容器的Web应用程序。 It's overkill if all you need is RestTemplate . 如果您只需要RestTemplate Replace the dependency with one on org.springframework:spring-web org.springframework:spring-web上的一个依赖项替换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM