Java Code Examples for io.swagger.util.ReflectionUtils#getAnnotation()

The following examples show how to use io.swagger.util.ReflectionUtils#getAnnotation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DubboReaderExtension.java    From swagger-dubbo with Apache License 2.0 6 votes vote down vote up
@Override
public void applyConsumes(ReaderContext context, Operation operation, Method method) {
	final List<String> consumes = new ArrayList<String>();
	final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);

	if (apiOperation != null) {
		consumes.addAll(parseStringValues(apiOperation.consumes()));
	}

	if (consumes.isEmpty()) {
		final Api apiAnnotation = context.getCls().getAnnotation(Api.class);
		if (apiAnnotation != null) {
			consumes.addAll(parseStringValues(apiAnnotation.consumes()));
		}
		consumes.addAll(context.getParentConsumes());
	}

	for (String consume : consumes) {
		operation.consumes(consume);
	}

}
 
Example 2
Source File: ControllerReaderExtension.java    From jboot with Apache License 2.0 6 votes vote down vote up
public void applySchemes(ReaderContext context, Operation operation, Method method) {
    final List<Scheme> schemes = new ArrayList<Scheme>();
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    final Api apiAnnotation = context.getCls().getAnnotation(Api.class);

    if (apiOperation != null) {
        schemes.addAll(parseSchemes(apiOperation.protocols()));
    }

    if (schemes.isEmpty() && apiAnnotation != null) {
        schemes.addAll(parseSchemes(apiAnnotation.protocols()));
    }

    for (Scheme scheme : schemes) {
        operation.scheme(scheme);
    }
}
 
Example 3
Source File: DubboReaderExtension.java    From swagger-dubbo with Apache License 2.0 6 votes vote down vote up
@Override
public void applySchemes(ReaderContext context, Operation operation, Method method) {
	final List<Scheme> schemes = new ArrayList<Scheme>();
	final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
	final Api apiAnnotation = context.getCls().getAnnotation(Api.class);

	if (apiOperation != null) {
		schemes.addAll(parseSchemes(apiOperation.protocols()));
	}

	if (schemes.isEmpty() && apiAnnotation != null) {
		schemes.addAll(parseSchemes(apiAnnotation.protocols()));
	}

	for (Scheme scheme : schemes) {
		operation.scheme(scheme);
	}

}
 
Example 4
Source File: DubboReaderExtension.java    From swagger-dubbo with Apache License 2.0 6 votes vote down vote up
@Override
public void applyProduces(ReaderContext context, Operation operation, Method method) {
	final List<String> produces = new ArrayList<String>();
	final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);

	if (apiOperation != null) {
		produces.addAll(parseStringValues(apiOperation.produces()));
	}

	if (produces.isEmpty()) {
		final Api apiAnnotation = context.getCls().getAnnotation(Api.class);
		if (apiAnnotation != null) {
			produces.addAll(parseStringValues(apiAnnotation.produces()));
		}
		produces.addAll(context.getParentProduces());
	}

	for (String produce : produces) {
		operation.produces(produce);
	}

}
 
Example 5
Source File: ControllerReaderExtension.java    From jboot with Apache License 2.0 6 votes vote down vote up
public void applySecurityRequirements(ReaderContext context, Operation operation, Method method) {
    final List<SecurityRequirement> securityRequirements = new ArrayList<SecurityRequirement>();
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    final Api apiAnnotation = context.getCls().getAnnotation(Api.class);

    if (apiOperation != null) {
        securityRequirements.addAll(parseAuthorizations(apiOperation.authorizations()));
    }

    if (securityRequirements.isEmpty() && apiAnnotation != null) {
        securityRequirements.addAll(parseAuthorizations(apiAnnotation.authorizations()));
    }

    for (SecurityRequirement securityRequirement : securityRequirements) {
        operation.security(securityRequirement);
    }
}
 
Example 6
Source File: RpcReaderExtension.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public void applySchemes(ReaderContext context, Operation operation, Method method) {
    final List<Scheme> schemes = new ArrayList<Scheme>();
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    final Api apiAnnotation = context.getCls().getAnnotation(Api.class);

    if (apiOperation != null) {
        schemes.addAll(parseSchemes(apiOperation.protocols()));
    }

    if (schemes.isEmpty() && apiAnnotation != null) {
        schemes.addAll(parseSchemes(apiAnnotation.protocols()));
    }

    for (Scheme scheme : schemes) {
        operation.scheme(scheme);
    }

}
 
Example 7
Source File: ControllerReaderExtension.java    From jboot with Apache License 2.0 6 votes vote down vote up
public void applyProduces(ReaderContext context, Operation operation, Method method) {
    final List<String> produces = new ArrayList<String>();
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);

    if (apiOperation != null) {
        produces.addAll(parseStringValues(apiOperation.produces()));
    }

    if (produces.isEmpty()) {
        final Api apiAnnotation = context.getCls().getAnnotation(Api.class);
        if (apiAnnotation != null) {
            produces.addAll(parseStringValues(apiAnnotation.produces()));
        }
        produces.addAll(context.getParentProduces());
    }

    for (String produce : produces) {
        operation.produces(produce);
    }
}
 
Example 8
Source File: RpcReaderExtension.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public void applyProduces(ReaderContext context, Operation operation, Method method) {
    final List<String> produces = new ArrayList<String>();
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);

    if (apiOperation != null) {
        produces.addAll(parseStringValues(apiOperation.produces()));
    }

    if (produces.isEmpty()) {
        final Api apiAnnotation = context.getCls().getAnnotation(Api.class);
        if (apiAnnotation != null) {
            produces.addAll(parseStringValues(apiAnnotation.produces()));
        }
        produces.addAll(context.getParentProduces());
    }

    for (String produce : produces) {
        operation.produces(produce);
    }

}
 
Example 9
Source File: RpcReaderExtension.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void applyDescription(Operation operation, Method method) {
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    if (apiOperation != null && StringUtils.isNotBlank(apiOperation.notes())) {
        operation.description(apiOperation.notes());
    }
    operation.description(operation.getDescription() == null ? outputMethod(method)
        : (outputMethod(method) + operation.getDescription()));
}
 
Example 10
Source File: RpcReaderExtension.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void applySummary(Operation operation, Method method) {
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    if (apiOperation != null && StringUtils.isNotBlank(apiOperation.value())) {
        operation.summary(apiOperation.value());
    }

}
 
Example 11
Source File: RpcReaderExtension.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public String getPath(ReaderContext context, Method method) {
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    String operationId = null == apiOperation ? ""
        : StringUtils.isBlank(apiOperation.nickname()) ? null : apiOperation.nickname();
    return PathUtils.collectPath(context.getParentPath(), context.getInterfaceCls().getName(),
        method.getName(), operationId);

}
 
Example 12
Source File: Reader.java    From dorado with Apache License 2.0 5 votes vote down vote up
private void readExternalDocs(Method method, Operation operation) {
	io.swagger.annotations.ExternalDocs externalDocs = ReflectionUtils.getAnnotation(method,
			io.swagger.annotations.ExternalDocs.class);
	if (externalDocs != null) {
		operation.setExternalDocs(new ExternalDocs(externalDocs.value(), externalDocs.url()));
	}
}
 
Example 13
Source File: ControllerReaderExtension.java    From jboot with Apache License 2.0 5 votes vote down vote up
public String getPath(ReaderContext context, Method method) {
    final Api apiAnnotation = context.getCls().getAnnotation(Api.class);
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    final String operationPath = apiOperation == null ? null : apiOperation.nickname();
    return PathUtils.collectPath(context.getParentPath(),
            apiAnnotation == null ? null : apiAnnotation.value(),
            StringUtils.isBlank(operationPath) ? method.getName() : operationPath);
}
 
Example 14
Source File: DubboReaderExtension.java    From swagger-dubbo with Apache License 2.0 5 votes vote down vote up
@Override
public void applySummary(Operation operation, Method method) {
	final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
	if (apiOperation != null && StringUtils.isNotBlank(apiOperation.value())) {
		operation.summary(apiOperation.value());
	}

}
 
Example 15
Source File: DubboReaderExtension.java    From swagger-dubbo with Apache License 2.0 5 votes vote down vote up
@Override
public void applyDescription(Operation operation, Method method) {
	final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
	if (apiOperation != null && StringUtils.isNotBlank(apiOperation.notes())) {
		operation.description(apiOperation.notes());
	}
	operation.description(operation.getDescription() == null ? outputMethod(method)
			: (outputMethod(method) + operation.getDescription()));
}
 
Example 16
Source File: ControllerReaderExtension.java    From jboot with Apache License 2.0 5 votes vote down vote up
private static Type getResponseType(Method method) {
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    if (apiOperation != null && !ReflectionUtils.isVoid(apiOperation.response())) {
        return apiOperation.response();
    } else {
        return method.getGenericReturnType();
    }
}
 
Example 17
Source File: ResourceReaderExtension.java    From mdw with Apache License 2.0 5 votes vote down vote up
@Override
public String getHttpMethod(ReaderContext context, Method method) {
    ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    if (apiOperation != null && apiOperation.httpMethod() != null && !apiOperation.httpMethod().isEmpty()) {
        return apiOperation.httpMethod();
    }
    else {
        Class<?> declaringClass = method.getDeclaringClass();
        if (declaringClass.getPackage().getName().equals(BASE_REST_PKG))
            return null;

        // JAX-RS annotations
        GET get = ReflectionUtils.getAnnotation(method, GET.class);
        if (get != null)
            return "get";
        PUT put = ReflectionUtils.getAnnotation(method, PUT.class);
        if (put != null) {
            if (method.getName().equals("patch"))
                return "patch";
            return "put";
        }
        POST post = ReflectionUtils.getAnnotation(method, POST.class);
        if (post != null)
            return "post";
        DELETE delete = ReflectionUtils.getAnnotation(method, DELETE.class);
        if (delete != null)
            return "delete";

        // MDW REST
        if (RestService.class.isAssignableFrom(declaringClass))
            return method.getName();

        return null;
    }
}
 
Example 18
Source File: Reader.java    From proteus with Apache License 2.0 4 votes vote down vote up
private void readExternalDocs(Method method, Operation operation) {
    io.swagger.annotations.ExternalDocs externalDocs = ReflectionUtils.getAnnotation(method, io.swagger.annotations.ExternalDocs.class);
    if(externalDocs != null) {
        operation.setExternalDocs(new ExternalDocs(externalDocs.value(), externalDocs.url()));
    }
}
 
Example 19
Source File: ResourceReaderExtension.java    From mdw with Apache License 2.0 4 votes vote down vote up
@Override
public String getPath(ReaderContext context, Method method) {
    String p = null;
    Api apiAnnotation = context.getCls().getAnnotation(Api.class);
    ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    String operationPath = apiOperation == null ? null : apiOperation.nickname();
    if (operationPath != null && !operationPath.isEmpty()) {
        // same logic as ServletReaderExtension
        p = PathUtils.collectPath(context.getParentPath(),
                apiAnnotation == null ? null : apiAnnotation.value(), operationPath);
    }
    else {
        // try JAX-RS annotations
        Path parentPath = ReflectionUtils.getAnnotation(method.getDeclaringClass(), Path.class);
        if (parentPath != null && parentPath.value() != null && !parentPath.value().isEmpty()) {
            p = parentPath.value();
        }
        Path path = ReflectionUtils.getAnnotation(method, Path.class);
        if (path != null && path.value() != null && !path.value().isEmpty()) {
            if (p == null)
                p = path.value();
            else {
                if (path.value().startsWith("/"))
                    p += path.value();
                else
                    p = p + "/" + path.value();
            }
        }
        // check dynamic java, which has package-based pathing
        java.lang.Package pkg = method.getDeclaringClass().getPackage();
        if (p != null && "MDW".equals(pkg.getImplementationTitle())) {
            if (p.startsWith("/"))
                p = "/" + pkg.getName().replace('.', '/') + p;
            else
                p = "/" + pkg.getName().replace('.', '/') + "/" + p;
        }

        if (apiOperation != null) {
            ApiImplicitParams implicitParams = ReflectionUtils.getAnnotation(method, ApiImplicitParams.class);
            if (implicitParams != null && implicitParams.value() != null && implicitParams.value().length == 1) {
                ApiImplicitParam implicitParam = implicitParams.value()[0];
                if (implicitParam.name() != null && !"body".equals(implicitParam.paramType()) && !"query".equals(implicitParam.paramType()) && !"header".equals(implicitParam.paramType()) && !"form".equals(implicitParam.paramType()))
                    p += "/{" + implicitParam.name() + "}";
            }
        }
    }

    return p;
}
 
Example 20
Source File: ControllerReaderExtension.java    From jboot with Apache License 2.0 4 votes vote down vote up
public void applyDescription(Operation operation, Method method) {
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    if (apiOperation != null && StringUtils.isNotBlank(apiOperation.notes())) {
        operation.description(apiOperation.notes());
    }
}