下面列出了org.springframework.web.bind.annotation.RequestMapping#consumes ( ) 实例代码,或者点击链接到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;
}
}
protected void parseConsumes(MethodMetadata md, Method method, RequestMapping annotation) {
String[] serverConsumes = annotation.consumes();
String clientProduces = serverConsumes.length == 0 ? null : emptyToNull(serverConsumes[0]);
if (clientProduces != null) {
md.template().header(CONTENT_TYPE, clientProduces);
}
}
private void logFunctionArguments(String[] argNames, Object[] argValues, StringBuilder stringBuilder,
Annotation annotations[][], RequestMapping methodRequestMapping) {
boolean someArgNeedsSerialization = false;
if (methodRequestMapping != null) {
for (String consumes : methodRequestMapping.consumes()) {
if (consumes.equals(MediaType.APPLICATION_JSON_VALUE)) {
someArgNeedsSerialization = true;
break;
}
}
}
stringBuilder.append(" called with arguments: ");
for (int i = 0, length = argNames.length; i < length; ++i) {
boolean needsSerialization = false;
if (argValues[i] instanceof ByteArrayResource || argValues[i] instanceof MultipartFile) {
needsSerialization = true;
} else {
if (someArgNeedsSerialization) {
for (Annotation annotation : annotations[i]) {
if (annotation instanceof RequestBody) {
needsSerialization = true;
break;
}
}
}
}
stringBuilder.append(argNames[i]).append(": [");
if (needsSerialization) {
String argClassName = argValues[i] == null ? "NULL" : argValues[i].getClass().getName();
serialize(argValues[i], argClassName, stringBuilder);
} else {
stringBuilder.append(getScrubbedValue(argNames[i], argValues[i]));
}
stringBuilder.append("]").append(i == (length - 1) ? "" : ", ");
}
}
private void parseConsumes(MethodMetadata md, Method method,
RequestMapping annotation) {
String[] serverConsumes = annotation.consumes();
String clientProduces = serverConsumes.length == 0 ? null
: emptyToNull(serverConsumes[0]);
if (clientProduces != null) {
md.template().header(CONTENT_TYPE, clientProduces);
}
}
private void parseConsumes(MethodMetadata md, Method method,
RequestMapping annotation) {
String[] serverConsumes = annotation.consumes();
String clientProduces = serverConsumes.length == 0 ? null
: emptyToNull(serverConsumes[0]);
if (clientProduces != null) {
md.template().header(CONTENT_TYPE, clientProduces);
}
}
private ControllerInfo resolveNameAttribute(Class<?> controller) {
ControllerInfo info = new ControllerInfo();
boolean isRestController = isRestController(controller);
GetMapping getMapping = findAnnotation(controller, GetMapping.class);
if (getMapping != null) {
info.name = getMapping.name();
info.consumes = getMapping.consumes();
info.produces = resolveJsonInfo(isRestController, getMapping.produces());
return info;
}
PostMapping postMapping = findAnnotation(controller, PostMapping.class);
if (postMapping != null) {
info.name = postMapping.name();
info.consumes = postMapping.consumes();
info.produces = resolveJsonInfo(isRestController, postMapping.produces());
return info;
}
DeleteMapping deleteMapping = findAnnotation(controller, DeleteMapping.class);
if (deleteMapping != null) {
info.name = deleteMapping.name();
info.consumes = deleteMapping.consumes();
info.produces = resolveJsonInfo(isRestController, deleteMapping.produces());
return info;
}
PutMapping putMapping = findAnnotation(controller, PutMapping.class);
if (putMapping != null) {
info.name = putMapping.name();
info.consumes = putMapping.consumes();
info.produces = resolveJsonInfo(isRestController, putMapping.produces());
return info;
}
RequestMapping requestMapping = findAnnotation(controller, RequestMapping.class);
if (requestMapping != null) {
info.name = requestMapping.name();
info.consumes = requestMapping.consumes();
info.produces = resolveJsonInfo(isRestController, requestMapping.produces());
return info;
}
return null;
}
private void parseConsumes(MethodMetadata md, Method method,
RequestMapping annotation) {
String[] serverConsumes = annotation.consumes();
String clientProduces = serverConsumes.length == 0 ? null
: emptyToNull(serverConsumes[0]);
if (clientProduces != null) {
md.template().header(CONTENT_TYPE, clientProduces);
}
}
/**
* Check if handler method consumes json (and just json) messages to handle
* it
*
* @see org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#supportsInternal(org.springframework.web.method.HandlerMethod)
*/
@Override
protected boolean supportsInternal(HandlerMethod handlerMethod) {
// Get requestMapping annotation
RequestMapping requestMappingAnnotation = handlerMethod
.getMethodAnnotation(RequestMapping.class);
if (requestMappingAnnotation == null) {
// No annotation: don't handle
return false;
}
// Get consumes configuration
String[] consumes = requestMappingAnnotation.consumes();
if (consumes == null || consumes.length != 1) {
// No consumes configuration or multiple consumes: don't handle
return false;
}
// Check consume value
// TODO extract a constant
if (!"application/json".equals(consumes[0])) {
// Don't consumes json: don't handle
return false;
}
// Delegate on super for additional checks
return super.supportsInternal(handlerMethod);
}
private EndPointInfo resolveNameAttribute(OperationContext context) {
EndPointInfo info = new EndPointInfo();
info.controllerName = resolveApiName(context);
boolean isRestEndPoint = isRestEndPoint(context);
GetMapping getMapping = context.findAnnotation(GetMapping.class).orNull();
if (getMapping != null) {
info.name = getMapping.name();
info.consumes = getMapping.consumes();
info.produces = resolveJsonInfo(isRestEndPoint, getMapping.produces());
return info;
}
PostMapping postMapping = context.findAnnotation(PostMapping.class).orNull();
if (postMapping != null) {
info.name = postMapping.name();
info.consumes = postMapping.consumes();
info.produces = resolveJsonInfo(isRestEndPoint, postMapping.produces());
return info;
}
DeleteMapping deleteMapping = context.findAnnotation(DeleteMapping.class).orNull();
if (deleteMapping != null) {
info.name = deleteMapping.name();
info.consumes = deleteMapping.consumes();
info.produces = resolveJsonInfo(isRestEndPoint, deleteMapping.produces());
return info;
}
PutMapping putMapping = context.findAnnotation(PutMapping.class).orNull();
if (putMapping != null) {
info.name = putMapping.name();
info.consumes = putMapping.consumes();
info.produces = resolveJsonInfo(isRestEndPoint, putMapping.produces());
return info;
}
RequestMapping requestMapping = context.findAnnotation(RequestMapping.class).orNull();
if (requestMapping != null) {
info.name = requestMapping.name();
info.consumes = requestMapping.consumes();
info.produces = resolveJsonInfo(isRestEndPoint, requestMapping.produces());
return info;
}
return null;
}
/**
* Generated name-value pair of method's formal arguments. Appends the generated string in provided StringBuilder
*
* @param argNames String[] containing method's formal argument names Order of names must correspond to order on arg
* values in argValues.
* @param argValues String[] containing method's formal argument values. Order of values must correspond to order on
* arg names in argNames.
* @param stringBuilder the StringBuilder to append argument data to.
*/
private void logFunctionArguments(
@Nonnull String[] argNames,
@Nonnull Object[] argValues,
@Nonnull StringBuilder stringBuilder,
@Nonnull Annotation annotations[][],
@Nullable RequestMapping methodRequestMapping) {
boolean someArgNeedsSerialization = false;
if (methodRequestMapping != null) {
for (String consumes : methodRequestMapping.consumes()) {
if (consumes.equals(MediaType.APPLICATION_JSON_VALUE)) {
someArgNeedsSerialization = true;
break;
}
}
}
stringBuilder.append(" called with arguments: ");
for (int i = 0, length = argNames.length; i < length; ++i) {
boolean needsSerialization = false;
if (argValues[i] instanceof ByteArrayResource || argValues[i] instanceof MultipartFile) {
needsSerialization = true;
} else {
if (someArgNeedsSerialization) {
// We only need to serialize a param if @RequestBody annotation is found.
for (Annotation annotation : annotations[i]) {
if (annotation instanceof RequestBody) {
needsSerialization = true;
break;
}
}
}
}
stringBuilder.append(argNames[i]).append(": [");
if (needsSerialization) {
String argClassName = argValues[i] == null ? "NULL" : argValues[i].getClass().getName();
serialize(argValues[i], argClassName, stringBuilder);
} else {
stringBuilder.append(getScrubbedValue(argNames[i], argValues[i]));
}
stringBuilder.append("]").append(i == (length - 1) ? "" : ", ");
}
}
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;
}
如果文章对你有帮助,欢迎点击上方按钮打赏作者