本文主要是介绍【小工具】WebClient远程调用,返回值将Long类型转换为String,自定义注解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 1. 使用WebClient使用远程调用
- 2. 返回值将Long类型转换为String
- 3. 自定义注解
1. 使用WebClient使用远程调用
<!-- SpringBoot webflux -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
import org.springframework.http.HttpHeaders;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;import java.util.Map;
===========================================================================/*** 远程调用,发送post请求** @param pathUrl 请求的访问的路径 如:/art/approval* @param requestBody 请求体(也可以是自定义的某个实体类)* @return 请求返回的结果,用R接收*/
private R remotePostCall(String pathUrl, Map<String, String> requestBody) {WebClient client = WebClient.create();// 发起POST请求,并设置请求头和请求体Mono<R> responseMono = client.post().uri(uri + pathUrl)// 设置请求头.header(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8").header(HttpHeaders.AUTHORIZATION, "you token")// 设置请求体.body(BodyInserters.fromValue(requestBody)).retrieve()// 将返回结果转换为R类型.bodyToMono(R.class);R block = responseMono.block();return block;
}
这个请求是异步的。在代码中,使用了 Mono 来表示返回结果, Mono 是一个Reactive类型,它表示可能会在未来某个时间点返回的结果。通过使用 bodyToMono() 方法将返回结果转换为 Mono 类型,然后使用 block() 方法来
阻塞
等待结果的返回。这种异步的方式可以提高应用程序的性能和并发处理能力。
可以使用 subscribe()
方法来实现不阻塞等待结果的返回。通过调用 subscribe()
方法,可以注册一个回调函数来处理异步返回的结果。这样,可以在结果返回时执行相应的操作,不需要阻塞等待。
private void remotePostCall(String pathUrl, Map<String, String> requestBody) {WebClient client = WebClient.create();// 发起POST请求,并设置请求头和请求体client.post().uri(uri + pathUrl)// 设置请求头.header(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8").header(HttpHeaders.AUTHORIZATION, "you token")// 设置请求体.body(BodyInserters.fromValue(requestBody)).retrieve()// 处理返回结果.bodyToMono(R.class).subscribe(result -> {// 在这里处理异步返回的结果// 可以执行相应的操作System.out.println("异步结果:" + result);});
}
2. 返回值将Long类型转换为String
在分布式项目中,我们主键喜欢使用雪花Id,可能会遇到一些精度丢失或数据截断的问题,特别是在处理大整数时。将 Long 类型转换为 String 类型可以避免这些问题,并确保数据的准确性和完整性。
通过自定义Jackson的对象映射器行为,可以确保在分布式项目中处理长整型数据时的一致性和可靠性。
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class JacksonConfiguration {@Beanpublic Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {return builder -> {// 返回结果时,统一将 Long 转换成 Stringbuilder.serializerByType(Long.class, ToStringSerializer.instance);};}
}
3. 自定义注解
- 自定义注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** @Author shang tf* @createTime 2023/8/15 10:35* @Version 1.0.0* 需要成为数商后才能访问*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface HasPayAuth {}
- 切面拦截注解
import cn.creatoo.common.core.constant.Constants;
import cn.creatoo.common.security.utils.BdSecurityUtils;
import cn.creatoo.system.exception.NotPayAuthException;
import cn.creatoo.system.domain.BdTerminalUserAuth;
import cn.creatoo.system.mapper.BdTerminalUserAuthMapper;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.time.LocalDateTime;/*** @Author shang tf* @createTime 2023/8/15 10:41* @Version 1.0.0*/
@Aspect
@Component
public class HasPayAuthAspect {@Pointcut("@annotation(xx.xxxxx.system.annotation.HasPayAuth)")private void pointcut() {}@Autowiredprivate BdTerminalUserAuthMapper bdTerminalUserAuthMapper;@Around("pointcut()")public Object around(ProceedingJoinPoint joinPoint) throws Throwable {// 查询当前登录账号信息Long userId = BdSecurityUtils.getUserId();QueryWrapper<BdTerminalUserAuth> wrapper = new QueryWrapper<>();wrapper.eq("user_id", userId);BdTerminalUserAuth bdTerminalUserAuth = bdTerminalUserAuthMapper.selectOne(wrapper);if (ObjectUtil.isEmpty(bdTerminalUserAuth)){throw new NotPayAuthException("未提交认证,请先认证");}// 账号状态if (!bdTerminalUserAuth.getStatus().equals(Constants.AUTH_AUDIT_CERTIFIED)){throw new NotPayAuthException("账号未认证,请先认证");}// 判断账号是否被禁用if (bdTerminalUserAuth.getIsAble().equals(Constants.AUTH_IS_ABLE_OFF)){throw new NotPayAuthException("账号被禁用,请联系管理员");}// 放行return joinPoint.proceed(joinPoint.getArgs());}
}
- 自定义异常
自定义异常时需要继承RuntimeException
/*** @Author shang tf* @createTime 2023/8/15 10:58* @Version 1.0.0* 资质认证过期或未认证异常*/
public class NotPayAuthException extends RuntimeException {public NotPayAuthException() {}public NotPayAuthException(String message){super(message);}
}
这篇关于【小工具】WebClient远程调用,返回值将Long类型转换为String,自定义注解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!