Java Code Examples for org.springframework.web.bind.annotation.RequestMapping#consumes()

The following examples show how to use org.springframework.web.bind.annotation.RequestMapping#consumes() . 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: CrossOriginTests.java    From spring-analysis-note with MIT License 7 votes vote down vote up
@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;
	}
}
 
Example 2
Source File: RequestMappingInfoHandlerMappingTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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;
	}
}
 
Example 3
Source File: CrossOriginTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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;
	}
}
 
Example 4
Source File: RequestMappingInfoHandlerMappingTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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;
	}
}
 
Example 5
Source File: RequestMappingInfoHandlerMappingTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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;
	}
}
 
Example 6
Source File: CrossOriginTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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;
	}
}
 
Example 7
Source File: RequestMappingMethodHandlerMapping.java    From stategen with GNU Affero General Public License v3.0 6 votes vote down vote up
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);
}
 
Example 8
Source File: SpringMvcContract.java    From raptor with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 9
Source File: Jackson2RequestMappingHandlerAdapter.java    From gvnix with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 10
Source File: VenusSpringMvcContract.java    From venus-cloud-feign with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 11
Source File: DefaultControllerPlugin.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 12
Source File: SpringMvcContract.java    From spring-cloud-openfeign with Apache License 2.0 5 votes vote down vote up
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);
	}
}
 
Example 13
Source File: GenericControllerAspect.java    From summerframework with Apache License 2.0 5 votes vote down vote up
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) ? "" : ", ");
    }
}
 
Example 14
Source File: OpenFeignSpringMvcContract.java    From summerframework with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 15
Source File: DefaultEndPointPlugin.java    From BlogManagePlatform with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 16
Source File: GenericControllerAspect.java    From controller-logger with Apache License 2.0 4 votes vote down vote up
/**
 * 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) ? "" : ", ");
    }
}
 
Example 17
Source File: SpringMvcApiReader.java    From swagger-maven-plugin with Apache License 2.0 4 votes vote down vote up
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;
}