org.rx.net.http.RestClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxlib Show documentation
Show all versions of rxlib Show documentation
A set of utilities for Java
package org.rx.net.http;
import lombok.extern.slf4j.Slf4j;
import org.rx.bean.ProceedEventArgs;
import org.rx.core.*;
import org.rx.exception.InvalidException;
import org.rx.util.function.BiFunc;
import org.rx.util.function.Func;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.lang.reflect.Parameter;
import java.util.HashMap;
import java.util.Map;
import static org.rx.core.Extends.ifNull;
import static org.rx.core.Sys.*;
@Slf4j
public final class RestClient {
public static T facade(Class contract, String serverPrefixUrl, BiFunc checkResponse) {
RequestMapping baseMapping = contract.getAnnotation(RequestMapping.class);
String prefix = serverPrefixUrl + getFirstPath(baseMapping);
boolean defMethod = isPostMethod(baseMapping);
return proxy(contract, (m, p) -> {
RequestMapping pathMapping = m.getAnnotation(RequestMapping.class);
String path = getFirstPath(pathMapping);
if (Strings.isEmpty(path)) {
path = m.getName();
}
String reqUrl = prefix + path;
boolean doPost = Arrays.isEmpty(pathMapping.method()) ? defMethod : isPostMethod(pathMapping);
Parameter[] parameters = m.getParameters();
Func