#->1 定时任务
参考:www.bilibili.com/video/BV1KW…
Service下新建ScheduleService.java
package com.finance.providerscore.service;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service;
import java.util.Date;
/*
- Spring Boot 定时任务
- /
@Service
public class ScheduleService {
/
- second, minute, hour, day of month, month, day of week
- 0 * * * * MON-FRI
- */ @Scheduled(cron = "0 0/10 * * * *") // 每10min执行一次 public void runScheduleTask(){ System.out.println("阿喵的定时任务开启 ..." + new Date()); } } 启动类添加@EnableScheduling 开启基于注解的定时任务
package com.finance.providerscore;
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication @EnableScheduling // 开启基于注解的定时任务 @MapperScan("com.finance.providerscore.mapper") public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
} #->2 Java后台调用Get/Post方法
pom.xml添加 httpclient 依赖
org.apache.httpcomponents httpclient 4.5.13新建工具类SendRequetUtil.java
package com.finance.providerscore.utils;
import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map;
public class SendRequestUtil { /*** * @param url * @param param * @return 发送返回响应 * @throws IOException */ public static String sendPost(String url, String param) throws IOException {
System.out.println("正在进行POST请求....");
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
StringEntity stringEntity = new StringEntity(param, "utf-8");
httpPost.setEntity(stringEntity);
String result = null;
HttpResponse response = httpClient.execute(httpPost);
result = EntityUtils.toString(response.getEntity(), "utf-8");
return result;
}
/**
* 向指定URL发送GET方法的请求
* @param url 发送请求的URL
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return URL 所代表远程资源的响应结果
*/
public static String sendGet(String url, String param) {
System.out.println("正在进行Get请求....");
String result = "";
BufferedReader bufferedReader = null;
try {
String urlNameString = url + "?" + param;
URL realURL = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realURL.openConnection();
// 设置通用请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有的响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
} finally { // 使用finally块来关闭输入流
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
} 测试:
Controller层UserHandler.java
package com.finance.providerscore.controller;
import com.finance.providerscore.entity.User; import com.finance.providerscore.mapper.UserMapper; import com.finance.providerscore.service.UserService; import com.github.pagehelper.PageInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.Map;
@RestController public class UserHandler {
@Autowired
private UserMapper userMapper;
@Autowired
private UserService userService;
@GetMapping("/findAll")
public List<User> getUserList() {
return userMapper.findAll();
}
@GetMapping("/findById2")
public User getUser2(int id){
return userMapper.findById(id);
}
@PostMapping("/newsList")
public PageInfo<User> getNewsList() {
PageInfo pageInfo = userService.selectList(1, 5);
return pageInfo;
}
} [Get方式测试]
String res1 = SendRequestUtil.sendGet("http://localhost:8080/findAll",""); System.out.println(res1);
String res2 = SendRequestUtil.sendGet("http://localhost:8080/findById2", "id=5"); System.out.println(res2); [Post方式测试]
String resPost = SendRequestUtil.sendPost("http://localhost:8080/newsList", ""); System.out.println(resPost); 参考:blog.csdn.net/shmely/arti… ———————————————— 版权声明:本文为CSDN博主「爱吃香草冰淇淋的阿喵」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:blog.csdn.net/coralime/ar…