Search in sources :

Example 1 with UnsatisfiedServletRequestParameterException

use of org.springframework.web.bind.UnsatisfiedServletRequestParameterException in project spring-framework by spring-projects.

the class RequestMappingInfoHandlerMapping method handleNoMatch.

/**
	 * Iterate all RequestMappingInfo's once again, look if any match by URL at
	 * least and raise exceptions according to what doesn't match.
	 * @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> infos, String lookupPath, HttpServletRequest request) throws ServletException {
    PartialMatchHelper helper = new PartialMatchHelper(infos, request);
    if (helper.isEmpty()) {
        return null;
    }
    if (helper.hasMethodsMismatch()) {
        Set<String> methods = helper.getAllowedMethods();
        if (HttpMethod.OPTIONS.matches(request.getMethod())) {
            HttpOptionsHandler handler = new HttpOptionsHandler(methods);
            return new HandlerMethod(handler, HTTP_OPTIONS_HANDLE_METHOD);
        }
        throw new HttpRequestMethodNotSupportedException(request.getMethod(), methods);
    }
    if (helper.hasConsumesMismatch()) {
        Set<MediaType> mediaTypes = helper.getConsumableMediaTypes();
        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<>(mediaTypes));
    }
    if (helper.hasProducesMismatch()) {
        Set<MediaType> mediaTypes = helper.getProducibleMediaTypes();
        throw new HttpMediaTypeNotAcceptableException(new ArrayList<>(mediaTypes));
    }
    if (helper.hasParamsMismatch()) {
        List<String[]> conditions = helper.getParamConditions();
        throw new UnsatisfiedServletRequestParameterException(conditions, request.getParameterMap());
    }
    return null;
}
Also used : UnsatisfiedServletRequestParameterException(org.springframework.web.bind.UnsatisfiedServletRequestParameterException) HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) HandlerMethod(org.springframework.web.method.HandlerMethod) HttpMediaTypeNotSupportedException(org.springframework.web.HttpMediaTypeNotSupportedException) MediaType(org.springframework.http.MediaType) InvalidMediaTypeException(org.springframework.http.InvalidMediaTypeException)

Example 2 with UnsatisfiedServletRequestParameterException

use of org.springframework.web.bind.UnsatisfiedServletRequestParameterException in project spring-framework by spring-projects.

the class RequestMappingInfoHandlerMappingTests method getHandlerUnsatisfiedServletRequestParameterException.

// SPR-12854
@Test
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]));
    }
}
Also used : UnsatisfiedServletRequestParameterException(org.springframework.web.bind.UnsatisfiedServletRequestParameterException) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) List(java.util.List) Test(org.junit.Test)

Aggregations

UnsatisfiedServletRequestParameterException (org.springframework.web.bind.UnsatisfiedServletRequestParameterException)2 List (java.util.List)1 Test (org.junit.Test)1 InvalidMediaTypeException (org.springframework.http.InvalidMediaTypeException)1 MediaType (org.springframework.http.MediaType)1 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)1 HttpMediaTypeNotAcceptableException (org.springframework.web.HttpMediaTypeNotAcceptableException)1 HttpMediaTypeNotSupportedException (org.springframework.web.HttpMediaTypeNotSupportedException)1 HttpRequestMethodNotSupportedException (org.springframework.web.HttpRequestMethodNotSupportedException)1 HandlerMethod (org.springframework.web.method.HandlerMethod)1