无论谁想获得自己的名声,都应该隐藏起他的自负。——斯威夫特
我们在webflux
场景下可以使用webclient
依赖就包含在了webflux
中
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency>
|
这里使用的话:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| import cn.dev33.satoken.same.SaSameUtil; import cn.hutool.core.map.MapUtil; import cn.hutool.json.JSONUtil; import com.alibaba.nacos.common.utils.JacksonUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.stereotype.Service; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono;
import java.util.List;
@Service public class AuthService {
private final WebClient webClient;
@Autowired public AuthService(WebClient.Builder webClientBuilder) { this.webClient = webClientBuilder.baseUrl("http://auth-service/user").build(); }
public Mono<List<String>> getPermissionList(Object loginId, String loginType) { MultiValueMap<String, String> param = new LinkedMultiValueMap<>(2); param.add("loginId", loginId.toString()); param.add("loginType", loginType); return webClient.post() .uri("/getPermissionList") .headers(m -> { m.add(SaSameUtil.SAME_TOKEN, SaSameUtil.getToken()); }) .body(BodyInserters.fromFormData(param)) .retrieve() .bodyToFlux(String.class) .flatMap(str -> { Hsswonse res = JacksonUtils.toObj(str, HsswResp.class); return Flux.fromIterable((List<String>) res.getData()); }) .collectList() .doOnError(Throwable::printStackTrace); }
public Mono<List<String>> getRoleList(Object loginId) { MultiValueMap<String, String> param = new LinkedMultiValueMap<>(2); param.add("loginId", loginId.toString()); return webClient.post() .uri("/getRoleList") .headers(m -> { m.add(SaSameUtil.SAME_TOKEN, SaSameUtil.getToken()); }) .contentType(MediaType.APPLICATION_JSON) .body(Mono.just(JSONUtil.toJsonStr(MapUtil.of("loginId", loginId))), String.class) .retrieve() .bodyToFlux(String.class) .doOnError(Throwable::printStackTrace) .flatMap(str -> { HsswResponse = JacksonUtils.toObj(str, HsswResponse.cla return Flux.fromIterable((List<String>) res.getData()); }) .collectList() .doOnError(Throwable::printStackTrace); } }
|
剩下就可以向普通的service
一样调用就行