Search in sources :

Example 16 with RequestMethod

use of org.springframework.web.bind.annotation.RequestMethod in project spring-boot by spring-projects.

the class AdditionalHealthEndpointPathsWebFluxHandlerMapping method getRequestMappingInfo.

private RequestMappingInfo getRequestMappingInfo(WebOperation operation, String additionalPath) {
    WebOperationRequestPredicate predicate = operation.getRequestPredicate();
    String path = this.endpointMapping.createSubPath(additionalPath);
    RequestMethod method = RequestMethod.valueOf(predicate.getHttpMethod().name());
    String[] consumes = StringUtils.toStringArray(predicate.getConsumes());
    String[] produces = StringUtils.toStringArray(predicate.getProduces());
    return RequestMappingInfo.paths(path).methods(method).consumes(consumes).produces(produces).build();
}
Also used : WebOperationRequestPredicate(org.springframework.boot.actuate.endpoint.web.WebOperationRequestPredicate) RequestMethod(org.springframework.web.bind.annotation.RequestMethod)

Example 17 with RequestMethod

use of org.springframework.web.bind.annotation.RequestMethod in project commons by terran4j.

the class Api2DocUtils method toURL.

public static String toURL(ApiDocObject doc, String serverURL) {
    if (doc == null) {
        throw new NullPointerException("doc is null.");
    }
    boolean hasGetMethod = false;
    RequestMethod[] methods = doc.getMethods();
    if (methods != null) {
        for (RequestMethod method : methods) {
            if (method == RequestMethod.GET) {
                hasGetMethod = true;
            }
        }
    } else {
        hasGetMethod = true;
    }
    if (!hasGetMethod) {
        // TODO: 暂时不支持非 GET 的请求。
        return null;
    }
    String docURL = serverURL + doc.getPaths()[0];
    List<ApiParamObject> params = doc.getParams();
    Map<String, String> pathParams = new HashMap<>();
    Map<String, String> getParams = new HashMap<>();
    if (params != null) {
        for (ApiParamObject param : params) {
            if (param.getLocation() == ApiParamLocation.PathVariable) {
                String value = param.getSample().getValue();
                if (StringUtils.isEmpty(value)) {
                    value = param.getDataType().getDefault();
                }
                pathParams.put(param.getId(), value);
            }
            if (param.getLocation() == ApiParamLocation.RequestParam) {
                String value = param.getSample().getValue();
                if (StringUtils.isEmpty(value)) {
                    continue;
                }
                getParams.put(param.getId(), value);
            }
        }
    }
    if (pathParams.size() > 0) {
        docURL = Strings.format(docURL, new ValueSource<String, String>() {

            @Override
            public String get(String key) {
                String value = pathParams.get(key);
                if (value == null) {
                    return null;
                }
                return encode(value);
            }
        }, "{", "}", null);
    }
    if (getParams.size() > 0) {
        List<String> keys = new ArrayList<>();
        keys.addAll(getParams.keySet());
        keys.sort(new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        StringBuffer sb = new StringBuffer();
        for (String key : keys) {
            String value = getParams.get(key);
            if (sb.length() > 0) {
                sb.append("&");
            }
            sb.append(key).append("=").append(encode(value));
        }
        docURL = docURL + "?" + sb.toString();
    }
    return docURL;
}
Also used : RequestMethod(org.springframework.web.bind.annotation.RequestMethod) ApiParamObject(com.terran4j.commons.api2doc.domain.ApiParamObject) ValueSource(com.terran4j.commons.util.value.ValueSource)

Example 18 with RequestMethod

use of org.springframework.web.bind.annotation.RequestMethod in project commons by terran4j.

the class CurlBuilder method toCurl.

public static String toCurl(ApiDocObject docObject, String serverURL) {
    final List<ApiParamObject> allParams = docObject.getParams();
    final KeyedList<String, String> headers = new KeyedList<>();
    final KeyedList<String, String> params = new KeyedList<>();
    final KeyedList<String, String> cookies = new KeyedList<>();
    final Map<String, String> pathVars = new HashMap<>();
    if (allParams.size() > 0) {
        for (ApiParamObject param : allParams) {
            String key = param.getId();
            String value = param.getSample().getValue();
            if (StringUtils.isEmpty(value)) {
                Class<?> paramType = param.getSourceType();
                if (paramType == String.class) {
                    value = key;
                } else {
                    value = param.getDataType().getDefault();
                }
            }
            if (param.getLocation() == ApiParamLocation.RequestParam) {
                params.add(key, value);
            }
            if (param.getLocation() == ApiParamLocation.PathVariable) {
                pathVars.put(key, value);
            }
            if (param.getLocation() == ApiParamLocation.RequestHeader) {
                headers.add(key, value);
            }
            if (param.getLocation() == ApiParamLocation.CookieValue) {
                cookies.add(key, value);
            }
        }
    }
    StringBuilder sb = new StringBuilder("curl ");
    sb.append(enter);
    // 将 Header 参数拼接起来。
    if (headers.size() > 0) {
        for (int i = 0; i < headers.size(); i++) {
            String key = headers.getKey(i);
            String value = headers.get(i);
            sb.append(" -H \"").append(key).append(": ").append(value).append("\" ").append(enter);
        }
    }
    if (cookies.size() > 0) {
        sb.append(" -b \"");
        sb.append(joinText(cookies, ";", "="));
        sb.append("\" ").append(enter);
    }
    // 将 URL 中的 {xx} 变量用参数的示例值代替。
    String url = serverURL + docObject.getPaths()[0];
    if (pathVars.size() > 0) {
        ValueSource<String, String> vars = new ValueSource<String, String>() {

            @Override
            public String get(String key) {
                return encode(pathVars.get(key));
            }
        };
        url = Strings.format(url, vars, "{", "}", null);
    }
    // 将“参数”拼起来。
    if (params.size() > 0) {
        RequestMethod[] requestMethods = docObject.getMethods();
        if (requestMethods.length == 1 && requestMethods[0] == RequestMethod.POST) {
            sb.append(" -d \"").append(joinText(params, "&", "=")).append("\" ").append(enter);
        } else {
            url += ("?" + joinText(params, "&", "="));
        }
    }
    // 将 URL 拼接起来。
    sb.append(" \"").append(url).append("\"");
    String curl = sb.toString();
    if (log.isInfoEnabled()) {
        log.info("doc[{}]'s curl:\n{}", docObject.getId(), curl);
    }
    return curl;
}
Also used : HashMap(java.util.HashMap) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) KeyedList(com.terran4j.commons.util.value.KeyedList) ApiParamObject(com.terran4j.commons.api2doc.domain.ApiParamObject) ValueSource(com.terran4j.commons.util.value.ValueSource)

Example 19 with RequestMethod

use of org.springframework.web.bind.annotation.RequestMethod in project commons by terran4j.

the class ApiMetaService method toRequestMethods.

private String[] toRequestMethods(RequestMethod[] methods) {
    if (methods == null || methods.length == 0) {
        return new String[] {};
    }
    String[] results = new String[methods.length];
    for (int i = 0; i < methods.length; i++) {
        RequestMethod method = methods[i];
        results[i] = method.name();
    }
    return results;
}
Also used : RequestMethod(org.springframework.web.bind.annotation.RequestMethod)

Example 20 with RequestMethod

use of org.springframework.web.bind.annotation.RequestMethod in project commons by terran4j.

the class RetrofitCodeWriter method toParam.

private ParamInfo toParam(ApiParamObject srcParam, ApiDocObject doc, Set<String> imports) {
    ParamInfo param = new ParamInfo();
    String id = srcParam.getId();
    param.setId(id);
    String comment = srcParam.getComment().javadoc(1);
    param.setComment(comment);
    StringBuffer expression = new StringBuffer();
    RequestMethod requestMethod = doc.getMethods()[0];
    ApiParamLocation location = srcParam.getLocation();
    String annoName = toParamAnnoName(location, requestMethod);
    // 
    expression.append("@").append(annoName).append("(\"").append(id).append("\")");
    Class<?> paramClass = Classes.toWrapType(srcParam.getSourceType());
    CodeUtils.addImport(paramClass, imports);
    expression.append(" ").append(paramClass.getSimpleName());
    expression.append(" ").append(id);
    param.setExpression(expression.toString());
    return param;
}
Also used : RequestMethod(org.springframework.web.bind.annotation.RequestMethod)

Aggregations

RequestMethod (org.springframework.web.bind.annotation.RequestMethod)26 HandlerMethod (org.springframework.web.method.HandlerMethod)6 RequestMappingInfo (org.springframework.web.servlet.mvc.method.RequestMappingInfo)6 Method (java.lang.reflect.Method)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 ArrayList (java.util.ArrayList)4 List (java.util.List)3 Test (org.junit.jupiter.api.Test)3 Visitor (com.baidu.disconf.web.service.user.dto.Visitor)2 ApiParamObject (com.terran4j.commons.api2doc.domain.ApiParamObject)2 ValueSource (com.terran4j.commons.util.value.ValueSource)2 LinkedHashMap (java.util.LinkedHashMap)2 Set (java.util.Set)2 TreeSet (java.util.TreeSet)2 CrossOrigin (org.springframework.web.bind.annotation.CrossOrigin)2 CorsConfiguration (org.springframework.web.cors.CorsConfiguration)2 Application (cn.edu.zjnu.acm.judge.Application)1 MatcherWrapper (cn.edu.zjnu.acm.judge.util.MatcherWrapper)1 RoleResource (com.baidu.disconf.web.service.roleres.bo.RoleResource)1