Java中泛型方法重写方案及多服务类泛型重构咨询
Java中泛型方法重写方案及多服务类泛型重构咨询
看起来你正卡在典型的重复代码重构场景里——多个服务类做着一模一样的事,只是处理的请求体和响应体类型不同,这种情况用泛型抽象类绝对是最优解,我来给你梳理一套落地的方案:
核心思路
把所有重复的WebClient请求、反序列化逻辑抽离到一个泛型父类中,让各个子服务类只需要指定自己对应的Payload和Response类型,以及少量个性化配置(比如请求URI)即可。
具体实现步骤
1. 定义泛型抽象父类RestService
这个类会承载所有通用逻辑,同时通过泛型参数P(Payload)和R(Response)来适配不同的子服务:
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.web.reactive.function.client.WebClient; public abstract class RestService<P, R> { // 复用WebClient和ObjectMapper实例,避免每次创建新对象(Spring环境可直接注入) private final WebClient webClient; private final ObjectMapper objectMapper; // 构造方法注入依赖 protected RestService(WebClient webClient, ObjectMapper objectMapper) { this.webClient = webClient; this.objectMapper = objectMapper; } // 通用核心方法,子类无需重写(除非有特殊定制需求) public R getResponse(P payload) { // 这里是你原方法里完全重复的WebClient请求逻辑 String responseBody = webClient.post() .uri(getRequestUri()) // 调用子类实现的URI方法 .bodyValue(payload) .retrieve() .bodyToMono(String.class) .block(); // 若用反应式编程可替换为非阻塞逻辑 // 通用的反序列化逻辑 try { return objectMapper.readValue(responseBody, new TypeReference<R>() {}); } catch (JsonProcessingException e) { // 可替换为自定义业务异常 throw new RuntimeException("响应反序列化失败", e); } } // 抽象方法:让子类提供各自的请求URI protected abstract String getRequestUri(); }
2. 实现各个子服务类
每个子服务只需要继承泛型父类,指定具体的Payload和Response类型,实现请求URI即可:
// ServiceA示例 public class ServiceA extends RestService<PayloadA, ResponseA> { public ServiceA(WebClient webClient, ObjectMapper objectMapper) { super(webClient, objectMapper); } @Override protected String getRequestUri() { return "/api/service-a"; } } // ServiceB示例 public class ServiceB extends RestService<PayloadB, ResponseB> { public ServiceB(WebClient webClient, ObjectMapper objectMapper) { super(webClient, objectMapper); } @Override protected String getRequestUri() { return "/api/service-b"; } }
额外优化建议
- 避免重复实例化工具类:不要在方法里每次都
new ObjectMapper(),Spring环境下可以直接注入框架提供的单例ObjectMapper,WebClient同理。 - 预留扩展钩子:如果某个服务需要特殊逻辑(比如添加自定义请求头、特殊反序列化规则),可以在父类添加钩子方法(比如
protected void beforeRequest(WebClient.RequestBodySpec requestSpec)),让子类按需重写。 - 关于你提到的空接口:如果你的
Payload或Response没有共同行为,完全不需要定义空接口,直接用具体类作为泛型参数即可;如果有通用行为(比如都有requestId字段),可以定义父类或接口来统一处理。
备注:内容来源于stack exchange,提问作者iyers16
相关产品推荐
相关产品推荐

