Examples
Functional style of Soap API call in Java Springboot using Generics and Builder Pattern.
Sample Outcome
return soapBaseClient
.request(getUserService::getUser)
.payload(soapClientRequestMapper.toGetUserRequest(userId))
.then(soapClientResponseMapper::mapToGetUserResponse)
.fetchResponse();Java Code
@FunctionalInterface
public interface RequestMethod<P, R> {
R execute(P payload);
}import java.util.function.Function;
public class Request<RequestPayloadType, ResponsePayloadType> {
private final RequestMethod<RequestPayloadType, ResponsePayloadType> requestMethod;
private RequestPayloadType payload;
public Request(RequestMethod<RequestPayloadType, ResponsePayloadType> requestMethod) {
this.requestMethod = requestMethod;
}
public Request<RequestPayloadType, ResponsePayloadType> payload(RequestPayloadType payload) {
this.payload = payload;
return this;
}
public <ProcessedResponseType> RequestCallback<ResponsePayloadType, ProcessedResponseType> then(
Function<ResponsePayloadType, ProcessedResponseType> handler) {
return new RequestCallback<>(
() -> requestMethod.execute(payload),
handler
);
}
}Last updated