Spring boot 我想每3秒钟做一次post请求,以http://localhost:8080/markable/messages 在下面的springboot应用程序中

Spring boot 我想每3秒钟做一次post请求,以http://localhost:8080/markable/messages 在下面的springboot应用程序中,spring-boot,scheduled-tasks,Spring Boot,Scheduled Tasks,这些是我开发的文件,但是我仍然无法实现上述帖子。请告诉我哪里出了问题。我的代码与postman和springboot tomcat配合得很好,但是我想自动将post to/messages url发送到邮件 MessageController.java MessageService.java POM.xml 有些事情看起来真的很奇怪 为什么要在spring上导入入门项目?这毫无意义。 由于您的问题缺少应用程序类,请仔细检查是否添加了@EnableScheduling注释。 为什么你要通过Rest

这些是我开发的文件,但是我仍然无法实现上述帖子。请告诉我哪里出了问题。我的代码与postman和springboot tomcat配合得很好,但是我想自动将post to/messages url发送到邮件

MessageController.java

MessageService.java

POM.xml


有些事情看起来真的很奇怪

为什么要在spring上导入入门项目?这毫无意义。 由于您的问题缺少应用程序类,请仔细检查是否添加了@EnableScheduling注释。 为什么你要通过RestTemplate调用自己应用程序中的rest控制器?实际上,您可以将消息服务直接插入到FixedMessagePoster中,并每隔x秒添加这些调用。 如果您要在应用程序或配置类中创建一个bean@bean作为RestTemplate,那么您就不必在每次调用时添加所有MessageConverter。 在if-else周围加上花括号是一种很好的做法,即使你可以离开if-else。这将提高可读性。 在某些类中,您正在导入消息,而消息在任何地方都没有使用。 由于您在FixedMessagePoster中发布的消息仅在方法中本地使用,因此我强烈建议您将此方法也设置为本地方法,而不是类的成员。 永远不要使用System.out输出某些信息。只需使用Spring Boot中内置的记录器。提示:slf4j和您将不依赖于所使用的框架 您的异常处理并不理想。只把真正需要的东西包装成一个试一试。
欢迎来到堆栈溢出。不幸的是,您的代码很难阅读,因为它位于一个大的块中。你能重新编排你的问题并把它分解吗?还有,我没看到你的主课在那里。分享一个帖子会让你更容易看到正在发生的事情。我的想法是每n秒用一个post请求点击/messages url。。。
import java.util.HashSet;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import service.MessageService;
import model.Message;

@RestController
@RequestMapping("/markable")
public class MessageController {

    @Autowired 
    MessageService ms;

    @RequestMapping(value="/messages", method=RequestMethod.POST)
    public HttpStatus takeInput(@RequestBody Message input) {
        boolean flag=ms.addMissionId(input.getMissionId());
        System.out.println(flag);

        if(flag)
            return HttpStatus.CREATED;
        else 
            return HttpStatus.CONFLICT;
    }

    @RequestMapping("/messages/all")
    public HashSet<Integer> getAll() {
        return ms.getAll();
    }

    @RequestMapping("/messages/{id}")
    public int getMissionId(@PathVariable("id") String id)
    {
        int inputId = Integer.parseInt(id);
        return ms.getMissionId(inputId);
    }
}
package poster;
import model.Message;
import org.apache.commons.logging.Log;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

@Component
public class FixedRateMessagePoster {

    Message m;
    int count=0;

    @Scheduled(fixedRate=3000)
    public void doPost()
    {
        try {
            m=new Message();
            m.setMissionId(count);
            count++;
            int seed = 1000+(int)(Math.random()*(20000-1000) +1);
            m.setSeed(seed);

            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
            String uri = new String("http://localhost:8080/markable/messages");

            restTemplate.postForLocation(uri,m);

            System.out.println("hello");
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
        }
        catch(Exception e) {
           e.printStackTrace();
        }
    }
}
package service;

import java.util.HashSet;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import model.Message;
import controller.MessageController;

@Service
public class MessageService {

    static HashSet<Integer> missionIds = new HashSet<Integer>();

    public HashSet<Integer> getAll(){
        return missionIds;
    }

    public boolean addMissionId(int missionId) {
        return missionIds.add(missionId);
    }

    public int getMissionId(int id){
        if(missionIds.contains(id))
            return id;
        else 
            return -1;
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Markable</name>
    <description>Spring Boot Application for Markable</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.spring.framework</groupId>
            <artifactId>gs-scheduling-tasks</artifactId>
            <version>0.1.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>