Spring 启动请求与时间表的映射

Spring boot Request Mapping with Schedule

我是 spring 引导的新手。我已经在 json.

中实现了一些成功输出的请求映射

localhost:8080/gJson

 {
    ad: "Windows 10",
    mimari: "amd64",
    versiyon: "10.0",
    diskSize: 918,
    freediskSize: 614,
    cores: 8,
    usablediskSize: 614
    }

这里是我的控制器

@EnableAutoConfiguration
@Controller      
public class GreetingController {

     @RequestMapping(value = "/gJson", produces=MediaType.APPLICATION_JSON_VALUE)
     public @ResponseBody  MyPojo gJson(){
         ...
     }
}

现在,我需要...示例,当我要执行此 link > localhost:8080/GetInfolocalhost:8080/gJson 获取 json 但每个 [=26= 】 秒。

感谢您的帮助。

如何提供 /GetInfo?它只是一个标准的 HTML 页面吗?如果是这样,您可以将具有 setInterval() to make an XMLHttpRequest 的 Javascript 元素编码到 /gJson 端点。根据您要使用哪些库进行浏览器到服务器通信,还有许多其他方法可以做到这一点。

* 更新 * 示例项目:https://github.com/ShawnTuatara/Whosebug-38890600

允许刷新的主要方面是 src/main/resources/static/GetInfo.html

的 HTML 页面
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>GetInfo</title>
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<body>

</body>
<script type="text/javascript">
    $(function() {
        window.setInterval(function() {
            $.ajax({
                url : "/gJson"
            }).done(function(data, status, jqXHR) {
                $("body").text(jqXHR.responseText);
            });
        }, 10000);
    });
</script>
</html>

如问题中所述,控制器很简单。

@EnableAutoConfiguration
@RestController
public class GreetingController {
    @GetMapping(value = "/gJson", produces = MediaType.APPLICATION_JSON_VALUE)
    public MyPojo gJson() {
        return new MyPojo("Windows 10", System.currentTimeMillis());
    }
}

最后,MyPojo 只是一个简单的两个字段 class。

public class MyPojo {
    private String ad;
    private long timestamp;

    public MyPojo(String ad, long timestamp) {
        this.ad = ad;
        this.timestamp = timestamp;
    }

    public String getAd() {
        return ad;
    }

    public void setAd(String ad) {
        this.ad = ad;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
}

我包含了时间戳,以便您可以在网页上看到每 10 秒刷新一次的时间。