java—我想在我的spring boot应用程序中使用调度程序调用api

ohtdti5x  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(205)

我想开发一个非webspring引导应用程序,我想在linux中作为服务运行。它将以1秒的间隔调用api。我可以调用api并将json作为字符串。但是当我把这个传给我的班级时,我得到了下面的错误。

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-12-06 00:36:05.829 ERROR 19816 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mslApiAgentApplication': Initialization of bean failed; nested exception is java.lang.IllegalStateException: Encountered invalid @Scheduled method 'printResponse': Only no-arg methods may be annotated with @Scheduled
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:617) ~[spring-beans-5.3.1.jar:5.3.1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:531) ~[spring-beans-5.3.1.jar:5.3.1]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.1.jar:5.3.1]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.1.jar:5.3.1]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.1.jar:5.3.1]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.1.jar:5.3.1]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.1.jar:5.3.1]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925) ~[spring-context-5.3.1.jar:5.3.1]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:588) ~[spring-context-5.3.1.jar:5.3.1]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.0.jar:2.4.0]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767) ~[spring-boot-2.4.0.jar:2.4.0]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) ~[spring-boot-2.4.0.jar:2.4.0]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) ~[spring-boot-2.4.0.jar:2.4.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) ~[spring-boot-2.4.0.jar:2.4.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309) ~[spring-boot-2.4.0.jar:2.4.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298) ~[spring-boot-2.4.0.jar:2.4.0]
    at com.mslapiagent.MslApiAgentApplication.main(MslApiAgentApplication.java:21) ~[classes/:na]
Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'printResponse': Only no-arg methods may be annotated with @Scheduled
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.processScheduled(ScheduledAnnotationBeanPostProcessor.java:511) ~[spring-context-5.3.1.jar:5.3.1]
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$null$1(ScheduledAnnotationBeanPostProcessor.java:374) ~[spring-context-5.3.1.jar:5.3.1]
    at java.base/java.lang.Iterable.forEach(Iterable.java:75) ~[na:na]
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$postProcessAfterInitialization$2(ScheduledAnnotationBeanPostProcessor.java:374) ~[spring-context-5.3.1.jar:5.3.1]
    at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) ~[na:na]
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(ScheduledAnnotationBeanPostProcessor.java:373) ~[spring-context-5.3.1.jar:5.3.1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:444) ~[spring-beans-5.3.1.jar:5.3.1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792) ~[spring-beans-5.3.1.jar:5.3.1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:609) ~[spring-beans-5.3.1.jar:5.3.1]
    ... 16 common frames omitted

我的其他代码如下:

package com.mslapiagent;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import com.mslapiagent.entity.MSLApiAgent;

@SpringBootApplication
@EnableScheduling
public class MslApiAgentApplication {

    String baseURL = "http://localhost:8080/MSLSystem_3/api/v1/messages";

    public static void main(String[] args) {
        SpringApplication.run(MslApiAgentApplication.class, args);
    }

    @Scheduled(fixedRate = 1000)
    public ResponseEntity<MSLApiAgent> printResponse(MSLApiAgent mslApiAgent) {
        MSLServices mslServices = new MSLServices();
        ResponseEntity<MSLApiAgent> printResponse = mslServices.printResponse(mslApiAgent);

        System.out.println(printResponse);
        return printResponse;
    }

}

package com.mslapiagent;

import java.util.Collections;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.mslapiagent.entity.MSLApiAgent;

public class MSLServices {

    private static String baseURL = "http://localhost:8080/MSLSystem_3/api/v1/messages";

    @Autowired
    RestTemplate restTemplate = new RestTemplate();

    public ResponseEntity<MSLApiAgent> printResponse(MSLApiAgent mslApiAgent) {

        HttpHeaders headers = new HttpHeaders();

        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

        headers.set("X-Request-Source", "Desktop");

        HttpEntity request = new HttpEntity(headers);

        ResponseEntity<MSLApiAgent> exchange = restTemplate.exchange(baseURL, HttpMethod.GET, request, MSLApiAgent.class);
        return exchange;
    }

}

下面是我的api类:

package com.mslapiagent.entity;

import java.io.Serializable;
import java.math.BigInteger;

public class MSLApiAgent implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private int id;
    private BigInteger tranId;
    private String clientTranId;
    private String msisdn;
    private String msgbody;

    public MSLApiAgent() {
    }
    public MSLApiAgent(int id, BigInteger tranId, String clientTranId, String msisdn, String msgbody) {
        this.id = id;
        this.tranId = tranId;
        this.clientTranId = clientTranId;
        this.msisdn = msisdn;
        this.msgbody = msgbody;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public BigInteger getTranId() {
        return tranId;
    }
    public void setTranId(BigInteger tranId) {
        this.tranId = tranId;
    }
    public String getClientTranId() {
        return clientTranId;
    }
    public void setClientTranId(String clientTranId) {
        this.clientTranId = clientTranId;
    }
    public String getMsisdn() {
        return msisdn;
    }
    public void setMsisdn(String msisdn) {
        this.msisdn = msisdn;
    }
    public String getMsgbody() {
        return msgbody;
    }
    public void setMsgbody(String msgbody) {
        this.msgbody = msgbody;
    }
    @Override
    public String toString() {
        return "MSLApiAgent [id=" + id + ", tranId=" + tranId + ", clientTranId=" + clientTranId + ", msisdn=" + msisdn
                + ", msgbody=" + msgbody + "]";
    }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题