下面列出了org.springframework.web.bind.annotation.RequestMapping#produces ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
RequestMapping annotation = AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class);
if (annotation != null) {
return new RequestMappingInfo(
new PatternsRequestCondition(annotation.value(), getUrlPathHelper(), getPathMatcher(), true, true),
new RequestMethodsRequestCondition(annotation.method()),
new ParamsRequestCondition(annotation.params()),
new HeadersRequestCondition(annotation.headers()),
new ConsumesRequestCondition(annotation.consumes(), annotation.headers()),
new ProducesRequestCondition(annotation.produces(), annotation.headers()), null);
}
else {
return null;
}
}
@Override
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
RequestMapping annot = AnnotationUtils.findAnnotation(method, RequestMapping.class);
if (annot != null) {
return new RequestMappingInfo(
new PatternsRequestCondition(annot.value(), getUrlPathHelper(), getPathMatcher(), true, true),
new RequestMethodsRequestCondition(annot.method()),
new ParamsRequestCondition(annot.params()),
new HeadersRequestCondition(annot.headers()),
new ConsumesRequestCondition(annot.consumes(), annot.headers()),
new ProducesRequestCondition(annot.produces(), annot.headers()), null);
}
else {
return null;
}
}
@Override
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
RequestMapping annotation = AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class);
if (annotation != null) {
return new RequestMappingInfo(
new PatternsRequestCondition(annotation.value(), getUrlPathHelper(), getPathMatcher(), true, true),
new RequestMethodsRequestCondition(annotation.method()),
new ParamsRequestCondition(annotation.params()),
new HeadersRequestCondition(annotation.headers()),
new ConsumesRequestCondition(annotation.consumes(), annotation.headers()),
new ProducesRequestCondition(annotation.produces(), annotation.headers()), null);
}
else {
return null;
}
}
@Override
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
RequestMapping annot = AnnotationUtils.findAnnotation(method, RequestMapping.class);
if (annot != null) {
return new RequestMappingInfo(
new PatternsRequestCondition(annot.value(), getUrlPathHelper(), getPathMatcher(), true, true),
new RequestMethodsRequestCondition(annot.method()),
new ParamsRequestCondition(annot.params()),
new HeadersRequestCondition(annot.headers()),
new ConsumesRequestCondition(annot.consumes(), annot.headers()),
new ProducesRequestCondition(annot.produces(), annot.headers()), null);
}
else {
return null;
}
}
protected RequestMappingInfo createRequestMappingInfoByApiMethodAnno(RequestMapping requestMapping, RequestCondition<?> customCondition,
Method method) {
String[] patterns = resolveEmbeddedValuesInPatterns(requestMapping.value());
if (!method.isAnnotationPresent(RequestMapping.class) || CollectionUtil.isEmpty(patterns)) {
RequestMappingResolveResult methodParsered = RequestMappingResolver.resolveOwnPath(method);
String path = methodParsered.getPath();
patterns = new String[] { path };
}
return new RequestMappingInfo(
new PatternsRequestCondition(patterns, getUrlPathHelper(), getPathMatcher(), this.useSuffixPatternMatch, this.useTrailingSlashMatch,
this.fileExtensions),
new RequestMethodsRequestCondition(requestMapping.method()), new ParamsRequestCondition(requestMapping.params()),
new HeadersRequestCondition(requestMapping.headers()), new ConsumesRequestCondition(requestMapping.consumes(), requestMapping.headers()),
new ProducesRequestCondition(requestMapping.produces(), requestMapping.headers(), this.contentNegotiationManager), customCondition);
}
@Override
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
RequestMapping annotation = AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class);
if (annotation != null) {
return new RequestMappingInfo(
new PatternsRequestCondition(annotation.value(), getUrlPathHelper(), getPathMatcher(), true, true),
new RequestMethodsRequestCondition(annotation.method()),
new ParamsRequestCondition(annotation.params()),
new HeadersRequestCondition(annotation.headers()),
new ConsumesRequestCondition(annotation.consumes(), annotation.headers()),
new ProducesRequestCondition(annotation.produces(), annotation.headers()), null);
}
else {
return null;
}
}
@Override
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
RequestMapping annotation = AnnotationUtils.findAnnotation(method, RequestMapping.class);
if (annotation != null) {
return new RequestMappingInfo(
new PatternsRequestCondition(annotation.value(), getUrlPathHelper(), getPathMatcher(), true, true),
new RequestMethodsRequestCondition(annotation.method()),
new ParamsRequestCondition(annotation.params()),
new HeadersRequestCondition(annotation.headers()),
new ConsumesRequestCondition(annotation.consumes(), annotation.headers()),
new ProducesRequestCondition(annotation.produces(), annotation.headers()), null);
}
else {
return null;
}
}
/**
* Build generic response.
*
* @param components the components
* @param findControllerAdvice the find controller advice
*/
public void buildGenericResponse(Components components, Map<String, Object> findControllerAdvice) {
// ControllerAdvice
for (Map.Entry<String, Object> entry : findControllerAdvice.entrySet()) {
List<Method> methods = new ArrayList<>();
Object controllerAdvice = entry.getValue();
// get all methods with annotation @ExceptionHandler
Class<?> objClz = controllerAdvice.getClass();
if (org.springframework.aop.support.AopUtils.isAopProxy(controllerAdvice))
objClz = org.springframework.aop.support.AopUtils.getTargetClass(controllerAdvice);
ControllerAdviceInfo controllerAdviceInfo = new ControllerAdviceInfo(controllerAdvice);
Arrays.stream(objClz.getDeclaredMethods()).filter(m -> m.isAnnotationPresent(ExceptionHandler.class)).forEach(methods::add);
// for each one build ApiResponse and add it to existing responses
for (Method method : methods) {
if (!operationBuilder.isHidden(method)) {
RequestMapping reqMappringMethod = AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class);
String[] methodProduces = { springDocConfigProperties.getDefaultProducesMediaType() };
if (reqMappringMethod != null)
methodProduces = reqMappringMethod.produces();
Map<String, ApiResponse> controllerAdviceInfoApiResponseMap = controllerAdviceInfo.getApiResponseMap();
MethodParameter methodParameter = new MethodParameter(method, -1);
ApiResponses apiResponsesOp = new ApiResponses();
MethodAttributes methodAttributes = new MethodAttributes(methodProduces, springDocConfigProperties.getDefaultConsumesMediaType(),
springDocConfigProperties.getDefaultProducesMediaType(), controllerAdviceInfoApiResponseMap);
Map<String, ApiResponse> apiResponses = computeResponseFromDoc(components, methodParameter, apiResponsesOp, methodAttributes);
buildGenericApiResponses(components, methodParameter, apiResponsesOp, methodAttributes);
apiResponses.forEach(controllerAdviceInfoApiResponseMap::put);
}
}
controllerAdviceInfos.add(controllerAdviceInfo);
}
}
protected void parseProduces(MethodMetadata md, Method method, RequestMapping annotation) {
String[] serverProduces = annotation.produces();
String clientAccepts = serverProduces.length == 0 ? null : emptyToNull(serverProduces[0]);
if (clientAccepts != null) {
md.template().header(ACCEPT, clientAccepts);
}
}
private void parseProduces(MethodMetadata md, Method method,
RequestMapping annotation) {
String[] serverProduces = annotation.produces();
String clientAccepts = serverProduces.length == 0 ? null
: emptyToNull(serverProduces[0]);
if (clientAccepts != null) {
md.template().header(ACCEPT, clientAccepts);
}
}
private void parseProduces(MethodMetadata md, Method method,
RequestMapping annotation) {
String[] serverProduces = annotation.produces();
String clientAccepts = serverProduces.length == 0 ? null
: emptyToNull(serverProduces[0]);
if (clientAccepts != null) {
md.template().header(ACCEPT, clientAccepts);
}
}
private void parseProduces(MethodMetadata md, Method method,
RequestMapping annotation) {
String[] serverProduces = annotation.produces();
String clientAccepts = serverProduces.length == 0 ? null
: emptyToNull(serverProduces[0]);
if (clientAccepts != null) {
md.template().header(ACCEPT, clientAccepts);
}
}
public Swagger read(SpringResource resource) {
if (swagger == null) {
swagger = new Swagger();
}
List<Method> methods = resource.getMethods();
Map<String, Tag> tags = new HashMap<String, Tag>();
List<SecurityRequirement> resourceSecurities = new ArrayList<SecurityRequirement>();
// Add the description from the controller api
Class<?> controller = resource.getControllerClass();
RequestMapping controllerRM = findMergedAnnotation(controller, RequestMapping.class);
String[] controllerProduces = new String[0];
String[] controllerConsumes = new String[0];
if (controllerRM != null) {
controllerConsumes = controllerRM.consumes();
controllerProduces = controllerRM.produces();
}
if (controller.isAnnotationPresent(Api.class)) {
Api api = findMergedAnnotation(controller, Api.class);
if (!canReadApi(false, api)) {
return swagger;
}
tags = updateTagsForApi(null, api);
resourceSecurities = getSecurityRequirements(api);
}
resourcePath = resource.getControllerMapping();
//collect api from method with @RequestMapping
Map<String, List<Method>> apiMethodMap = collectApisByRequestMapping(methods);
for (String path : apiMethodMap.keySet()) {
for (Method method : apiMethodMap.get(path)) {
RequestMapping requestMapping = findMergedAnnotation(method, RequestMapping.class);
if (requestMapping == null) {
continue;
}
ApiOperation apiOperation = findMergedAnnotation(method, ApiOperation.class);
if (apiOperation != null && apiOperation.hidden()) {
continue;
}
Map<String, String> regexMap = new HashMap<String, String>();
String operationPath = parseOperationPath(path, regexMap);
//http method
for (RequestMethod requestMethod : requestMapping.method()) {
String httpMethod = requestMethod.toString().toLowerCase();
Operation operation = parseMethod(method, requestMethod);
updateOperationParameters(new ArrayList<Parameter>(), regexMap, operation);
updateOperationProtocols(apiOperation, operation);
String[] apiProduces = requestMapping.produces();
String[] apiConsumes = requestMapping.consumes();
apiProduces = (apiProduces.length == 0) ? controllerProduces : apiProduces;
apiConsumes = (apiConsumes.length == 0) ? controllerConsumes : apiConsumes;
apiConsumes = updateOperationConsumes(new String[0], apiConsumes, operation);
apiProduces = updateOperationProduces(new String[0], apiProduces, operation);
updateTagsForOperation(operation, apiOperation);
updateOperation(apiConsumes, apiProduces, tags, resourceSecurities, operation);
updatePath(operationPath, httpMethod, operation);
}
}
}
return swagger;
}
如果文章对你有帮助,欢迎点击上方按钮打赏作者