变更为webclient响应式客户端
This commit is contained in:
9
pom.xml
9
pom.xml
@@ -92,6 +92,15 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.projectreactor</groupId>
|
||||||
|
<artifactId>reactor-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<finalName>dailylove</finalName>
|
<finalName>dailylove</finalName>
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package com.bamanker.dailylove.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @descriptions TODO
|
||||||
|
* @author bamanker
|
||||||
|
* @date 2026/1/12 17:33
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class WebClientConfig {
|
||||||
|
/**
|
||||||
|
* 创建WebClient Bean
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public WebClient wechatWebClient() {
|
||||||
|
return WebClient.builder()
|
||||||
|
.baseUrl("https://api.weixin.qq.com/cgi-bin") // 基础URL
|
||||||
|
.defaultHeader("User-Agent", "WebFlux-Client") // 默认请求头
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public WebClient dateRemoteWebClient() {
|
||||||
|
return WebClient.builder()
|
||||||
|
.baseUrl("${tianxin.server}") // 基础URL
|
||||||
|
.defaultHeader("User-Agent", "WebFlux-Client") // 默认请求头
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
|
|
||||||
import tools.jackson.databind.JsonNode;
|
import tools.jackson.databind.JsonNode;
|
||||||
import tools.jackson.databind.ObjectMapper;
|
import tools.jackson.databind.ObjectMapper;
|
||||||
|
import tools.jackson.databind.json.JsonMapper;
|
||||||
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@@ -29,7 +30,7 @@ import java.util.Date;
|
|||||||
public class PushDailyController {
|
public class PushDailyController {
|
||||||
|
|
||||||
final
|
final
|
||||||
ObjectMapper mapper;
|
JsonMapper mapper;
|
||||||
|
|
||||||
final
|
final
|
||||||
DataRemoteClient dataRemoteClient;
|
DataRemoteClient dataRemoteClient;
|
||||||
@@ -37,7 +38,7 @@ public class PushDailyController {
|
|||||||
final
|
final
|
||||||
WechatRequestClient wechatRequestClient;
|
WechatRequestClient wechatRequestClient;
|
||||||
|
|
||||||
public PushDailyController(DataRemoteClient dataRemoteClient, WechatRequestClient wechatRequestClient, ObjectMapper mapper) {
|
public PushDailyController(DataRemoteClient dataRemoteClient, WechatRequestClient wechatRequestClient, JsonMapper mapper) {
|
||||||
this.dataRemoteClient = dataRemoteClient;
|
this.dataRemoteClient = dataRemoteClient;
|
||||||
this.wechatRequestClient = wechatRequestClient;
|
this.wechatRequestClient = wechatRequestClient;
|
||||||
this.mapper = mapper;
|
this.mapper = mapper;
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package com.bamanker.dailylove.service;
|
||||||
|
|
||||||
|
import com.bamanker.dailylove.domain.TianXinReqParam;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class DataRemoteService {
|
||||||
|
|
||||||
|
final
|
||||||
|
WebClient webClient;
|
||||||
|
|
||||||
|
public DataRemoteService(WebClient dateRemoteWebClient) {
|
||||||
|
this.webClient = dateRemoteWebClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mono<String> getWeather(@RequestParam TianXinReqParam param) {
|
||||||
|
return webClient.get()
|
||||||
|
.uri("/tianqi/index", param)
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mono<String> getRainbow(@RequestParam TianXinReqParam param) {
|
||||||
|
return webClient.get()
|
||||||
|
.uri("/caihongpi/index", param)
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mono<String> getDailyEnglish(@RequestParam TianXinReqParam param) {
|
||||||
|
return webClient.get()
|
||||||
|
.uri("/ensentence/index", param)
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mono<String> getTips(@RequestParam TianXinReqParam param) {
|
||||||
|
return webClient.get()
|
||||||
|
.uri("/qiaomen/index", param)
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mono<String> getMorning(@RequestParam TianXinReqParam param) {
|
||||||
|
return webClient.get()
|
||||||
|
.uri("/zaoan/index", param)
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mono<String> getNight(@RequestParam TianXinReqParam param) {
|
||||||
|
return webClient.get()
|
||||||
|
.uri("/wanan/index", param)
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(String.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.bamanker.dailylove.service;
|
||||||
|
|
||||||
|
import com.bamanker.dailylove.domain.ResultVo;
|
||||||
|
import com.bamanker.dailylove.domain.WechatTokenParam;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.cloud.openfeign.SpringQueryMap;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class WechatRequestService {
|
||||||
|
|
||||||
|
final
|
||||||
|
WebClient webClient;
|
||||||
|
|
||||||
|
public WechatRequestService(WebClient dateRemoteWebClient) {
|
||||||
|
this.webClient = dateRemoteWebClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mono<String> getAccessToken(@SpringQueryMap WechatTokenParam param){
|
||||||
|
return webClient.get()
|
||||||
|
.uri("/token", param)
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mono<String> sendMsg(String token, ResultVo resultVo){
|
||||||
|
return webClient.post()
|
||||||
|
.uri("/message/template/send?access_token={token}", token)
|
||||||
|
.bodyValue(resultVo)
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user