org.springframework.web.bind.UnsatisfiedServletRequestParameterException Java Examples

The following examples show how to use org.springframework.web.bind.UnsatisfiedServletRequestParameterException. 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: DefaultAnnotationHandlerMapping.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validate the given type-level mapping metadata against the current request,
 * checking HTTP request method and parameter conditions.
 * @param mapping the mapping metadata to validate
 * @param request current HTTP request
 * @throws Exception if validation failed
 */
protected void validateMapping(RequestMapping mapping, HttpServletRequest request) throws Exception {
	RequestMethod[] mappedMethods = mapping.method();
	if (!ServletAnnotationMappingUtils.checkRequestMethod(mappedMethods, request)) {
		String[] supportedMethods = new String[mappedMethods.length];
		for (int i = 0; i < mappedMethods.length; i++) {
			supportedMethods[i] = mappedMethods[i].name();
		}
		throw new HttpRequestMethodNotSupportedException(request.getMethod(), supportedMethods);
	}

	String[] mappedParams = mapping.params();
	if (!ServletAnnotationMappingUtils.checkParameters(mappedParams, request)) {
		throw new UnsatisfiedServletRequestParameterException(mappedParams, request.getParameterMap());
	}

	String[] mappedHeaders = mapping.headers();
	if (!ServletAnnotationMappingUtils.checkHeaders(mappedHeaders, request)) {
		throw new ServletRequestBindingException("Header conditions \"" +
				StringUtils.arrayToDelimitedString(mappedHeaders, ", ") +
				"\" not met for actual request");
	}
}
 
Example #2
Source File: DefaultAnnotationHandlerMapping.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Validate the given type-level mapping metadata against the current request,
 * checking HTTP request method and parameter conditions.
 * @param mapping the mapping metadata to validate
 * @param request current HTTP request
 * @throws Exception if validation failed
 */
protected void validateMapping(RequestMapping mapping, HttpServletRequest request) throws Exception {
	RequestMethod[] mappedMethods = mapping.method();
	if (!ServletAnnotationMappingUtils.checkRequestMethod(mappedMethods, request)) {
		String[] supportedMethods = new String[mappedMethods.length];
		for (int i = 0; i < mappedMethods.length; i++) {
			supportedMethods[i] = mappedMethods[i].name();
		}
		throw new HttpRequestMethodNotSupportedException(request.getMethod(), supportedMethods);
	}

	String[] mappedParams = mapping.params();
	if (!ServletAnnotationMappingUtils.checkParameters(mappedParams, request)) {
		throw new UnsatisfiedServletRequestParameterException(mappedParams, request.getParameterMap());
	}

	String[] mappedHeaders = mapping.headers();
	if (!ServletAnnotationMappingUtils.checkHeaders(mappedHeaders, request)) {
		throw new ServletRequestBindingException("Header conditions \"" +
				StringUtils.arrayToDelimitedString(mappedHeaders, ", ") +
				"\" not met for actual request");
	}
}
 
Example #3
Source File: RequestMappingInfoHandlerMappingTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test  // SPR-12854
public void getHandlerUnsatisfiedServletRequestParameterException() throws Exception {
	try {
		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/params");
		this.handlerMapping.getHandler(request);
		fail("UnsatisfiedServletRequestParameterException expected");
	}
	catch (UnsatisfiedServletRequestParameterException ex) {
		List<String[]> groups = ex.getParamConditionGroups();
		assertEquals(2, groups.size());
		assertThat(Arrays.asList("foo=bar", "bar=baz"),
				containsInAnyOrder(groups.get(0)[0], groups.get(1)[0]));
	}
}
 
Example #4
Source File: RequestMappingInfoHandlerMappingTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test  // SPR-12854
public void getHandlerUnsatisfiedServletRequestParameterException() throws Exception {
	try {
		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/params");
		this.handlerMapping.getHandler(request);
		fail("UnsatisfiedServletRequestParameterException expected");
	}
	catch (UnsatisfiedServletRequestParameterException ex) {
		List<String[]> groups = ex.getParamConditionGroups();
		assertEquals(2, groups.size());
		assertThat(Arrays.asList("foo=bar", "bar=baz"),
				containsInAnyOrder(groups.get(0)[0], groups.get(1)[0]));
	}
}
 
Example #5
Source File: RequestMappingInfoHandlerMappingTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnsatisfiedServletRequestParameterException() throws Exception {
	try {
		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/params");
		this.handlerMapping.getHandler(request);
		fail("UnsatisfiedServletRequestParameterException expected");
	}
	catch (UnsatisfiedServletRequestParameterException ex) {
		List<String[]> groups = ex.getParamConditionGroups();
		assertEquals(2, groups.size());
		assertThat(Arrays.asList("foo=bar", "bar=baz"),
				containsInAnyOrder(groups.get(0)[0], groups.get(1)[0]));
	}
}
 
Example #6
Source File: DefaultExceptionHandler.java    From bearchoke with Apache License 2.0 5 votes vote down vote up
@RequestMapping(produces = {ApplicationMediaType.APPLICATION_BEARCHOKE_V1_JSON_VALUE, ApplicationMediaType.APPLICATION_BEARCHOKE_V2_JSON_VALUE})
@ExceptionHandler({MissingServletRequestParameterException.class,
        UnsatisfiedServletRequestParameterException.class,
        HttpRequestMethodNotSupportedException.class,
        ServletRequestBindingException.class
})
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public @ResponseBody ErrorMessage handleRequestException(Exception ex) {
    log.error("Http Status: " + HttpStatus.BAD_REQUEST + " : " + ex.getMessage(), ex);
    return new ErrorMessage(new Date(), HttpStatus.BAD_REQUEST.value(), ex.getClass().getName(), ex.getMessage());
}
 
Example #7
Source File: DefaultExceptionHandler.java    From spring-rest-server with GNU Lesser General Public License v3.0 5 votes vote down vote up
@RequestMapping(produces = {Versions.V1_0, Versions.V2_0})
@ExceptionHandler({MissingServletRequestParameterException.class,
        UnsatisfiedServletRequestParameterException.class,
        HttpRequestMethodNotSupportedException.class,
        ServletRequestBindingException.class
})
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public @ResponseBody Map<String, Object> handleRequestException(Exception ex) {
    Map<String, Object>  map = Maps.newHashMap();
    map.put("error", "Request Error");
    map.put("cause", ex.getMessage());
    return map;
}
 
Example #8
Source File: RequestMappingInfoHandlerMapping.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Iterate all RequestMappingInfos once again, look if any match by URL at
 * least and raise exceptions accordingly.
 * @throws HttpRequestMethodNotSupportedException if there are matches by URL
 * but not by HTTP method
 * @throws HttpMediaTypeNotAcceptableException if there are matches by URL
 * but not by consumable/producible media types
 */
@Override
protected HandlerMethod handleNoMatch(Set<RequestMappingInfo> requestMappingInfos,
		String lookupPath, HttpServletRequest request) throws ServletException {

	Set<String> allowedMethods = new LinkedHashSet<String>(4);

	Set<RequestMappingInfo> patternMatches = new HashSet<RequestMappingInfo>();
	Set<RequestMappingInfo> patternAndMethodMatches = new HashSet<RequestMappingInfo>();

	for (RequestMappingInfo info : requestMappingInfos) {
		if (info.getPatternsCondition().getMatchingCondition(request) != null) {
			patternMatches.add(info);
			if (info.getMethodsCondition().getMatchingCondition(request) != null) {
				patternAndMethodMatches.add(info);
			}
			else {
				for (RequestMethod method : info.getMethodsCondition().getMethods()) {
					allowedMethods.add(method.name());
				}
			}
		}
	}

	if (patternMatches.isEmpty()) {
		return null;
	}
	else if (patternAndMethodMatches.isEmpty() && !allowedMethods.isEmpty()) {
		throw new HttpRequestMethodNotSupportedException(request.getMethod(), allowedMethods);
	}

	Set<MediaType> consumableMediaTypes;
	Set<MediaType> producibleMediaTypes;
	List<String[]> paramConditions;

	if (patternAndMethodMatches.isEmpty()) {
		consumableMediaTypes = getConsumableMediaTypes(request, patternMatches);
		producibleMediaTypes = getProducibleMediaTypes(request, patternMatches);
		paramConditions = getRequestParams(request, patternMatches);
	}
	else {
		consumableMediaTypes = getConsumableMediaTypes(request, patternAndMethodMatches);
		producibleMediaTypes = getProducibleMediaTypes(request, patternAndMethodMatches);
		paramConditions = getRequestParams(request, patternAndMethodMatches);
	}

	if (!consumableMediaTypes.isEmpty()) {
		MediaType contentType = null;
		if (StringUtils.hasLength(request.getContentType())) {
			try {
				contentType = MediaType.parseMediaType(request.getContentType());
			}
			catch (InvalidMediaTypeException ex) {
				throw new HttpMediaTypeNotSupportedException(ex.getMessage());
			}
		}
		throw new HttpMediaTypeNotSupportedException(contentType, new ArrayList<MediaType>(consumableMediaTypes));
	}
	else if (!producibleMediaTypes.isEmpty()) {
		throw new HttpMediaTypeNotAcceptableException(new ArrayList<MediaType>(producibleMediaTypes));
	}
	else if (!CollectionUtils.isEmpty(paramConditions)) {
		throw new UnsatisfiedServletRequestParameterException(paramConditions, request.getParameterMap());
	}
	else {
		return null;
	}
}
 
Example #9
Source File: RestControllerAdvice.java    From spring-cloud-dataflow with Apache License 2.0 4 votes vote down vote up
/**
 * Client did not formulate a correct request. Log the exception message at warn level
 * and stack trace as trace level. Return response status HttpStatus.BAD_REQUEST
 * (400).
 *
 * @param e one of the exceptions, {@link MissingServletRequestParameterException},
 * {@link UnsatisfiedServletRequestParameterException},
 * {@link MethodArgumentTypeMismatchException}, or
 * {@link InvalidStreamDefinitionException}
 * @return the error response in JSON format with media type
 * application/vnd.error+json
 */
@ExceptionHandler({ MissingServletRequestParameterException.class, HttpMessageNotReadableException.class,
		UnsatisfiedServletRequestParameterException.class, MethodArgumentTypeMismatchException.class,
		InvalidDateRangeException.class, CannotDeleteNonParentTaskExecutionException.class,
		InvalidStreamDefinitionException.class, CreateScheduleException.class, OffsetOutOfBoundsException.class,
		TaskExecutionMissingExternalIdException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public VndErrors onClientGenericBadRequest(Exception e) {
	String logref = logWarnLevelExceptionMessage(e);
	if (logger.isTraceEnabled()) {
		logTraceLevelStrackTrace(e);
	}

	String message = null;
	if (e instanceof MethodArgumentTypeMismatchException) {
		final MethodArgumentTypeMismatchException methodArgumentTypeMismatchException = (MethodArgumentTypeMismatchException) e;
		final Class<?> requiredType = methodArgumentTypeMismatchException.getRequiredType();

		final Class<?> enumType;

		if (requiredType.isEnum()) {
			enumType = requiredType;
		}
		else if (requiredType.isArray() && requiredType.getComponentType().isEnum()) {
			enumType = requiredType.getComponentType();
		}
		else {
			enumType = null;
		}

		if (enumType != null) {
			final String enumValues = StringUtils.arrayToDelimitedString(enumType.getEnumConstants(), ", ");
			message = String.format("The parameter '%s' must contain one of the following values: '%s'.", methodArgumentTypeMismatchException.getName(), enumValues);
		}
	}

	if (message == null) {
		message = getExceptionMessage(e);
	}

	return new VndErrors(logref, message);
}