use of org.springframework.web.server.NotAcceptableStatusException in project spring-framework by spring-projects.
the class RequestMappingInfoHandlerMapping method handleNoMatch.
/**
* Iterate all RequestMappingInfos once again, look if any match by URL at
* least and raise exceptions accordingly.
* @throws MethodNotAllowedException for matches by URL but not by HTTP method
* @throws UnsupportedMediaTypeStatusException if there are matches by URL
* and HTTP method but not by consumable media types
* @throws NotAcceptableStatusException if there are matches by URL and HTTP
* method but not by producible media types
* @throws ServerWebInputException if there are matches by URL and HTTP
* method but not by query parameter conditions
*/
@Override
protected HandlerMethod handleNoMatch(Set<RequestMappingInfo> infos, ServerWebExchange exchange) throws Exception {
PartialMatchHelper helper = new PartialMatchHelper(infos, exchange);
if (helper.isEmpty()) {
return null;
}
ServerHttpRequest request = exchange.getRequest();
if (helper.hasMethodsMismatch()) {
HttpMethod httpMethod = request.getMethod();
Set<HttpMethod> methods = helper.getAllowedMethods();
if (HttpMethod.OPTIONS.equals(httpMethod)) {
Set<MediaType> mediaTypes = helper.getConsumablePatchMediaTypes();
HttpOptionsHandler handler = new HttpOptionsHandler(methods, mediaTypes);
return new HandlerMethod(handler, HTTP_OPTIONS_HANDLE_METHOD);
}
throw new MethodNotAllowedException(httpMethod, methods);
}
if (helper.hasConsumesMismatch()) {
Set<MediaType> mediaTypes = helper.getConsumableMediaTypes();
MediaType contentType;
try {
contentType = request.getHeaders().getContentType();
} catch (InvalidMediaTypeException ex) {
throw new UnsupportedMediaTypeStatusException(ex.getMessage());
}
throw new UnsupportedMediaTypeStatusException(contentType, new ArrayList<>(mediaTypes), exchange.getRequest().getMethod());
}
if (helper.hasProducesMismatch()) {
Set<MediaType> mediaTypes = helper.getProducibleMediaTypes();
throw new NotAcceptableStatusException(new ArrayList<>(mediaTypes));
}
if (helper.hasParamsMismatch()) {
throw new ServerWebInputException("Unsatisfied query parameter conditions: " + helper.getParamConditions() + ", actual parameters: " + request.getQueryParams());
}
return null;
}
use of org.springframework.web.server.NotAcceptableStatusException in project spring-framework by spring-projects.
the class ViewResolutionResultHandler method render.
private Mono<? extends Void> render(List<View> views, Map<String, Object> model, BindingContext bindingContext, ServerWebExchange exchange) {
for (View view : views) {
if (view.isRedirectView()) {
return renderWith(view, model, null, exchange, bindingContext);
}
}
List<MediaType> mediaTypes = getMediaTypes(views);
MediaType bestMediaType;
try {
bestMediaType = selectMediaType(exchange, () -> mediaTypes);
} catch (NotAcceptableStatusException ex) {
HttpStatus statusCode = exchange.getResponse().getStatusCode();
if (statusCode != null && statusCode.isError()) {
if (logger.isDebugEnabled()) {
logger.debug("Ignoring error response content (if any). " + ex.getReason());
}
return Mono.empty();
}
throw ex;
}
if (bestMediaType != null) {
for (View view : views) {
for (MediaType mediaType : view.getSupportedMediaTypes()) {
if (mediaType.isCompatibleWith(bestMediaType)) {
return renderWith(view, model, mediaType, exchange, bindingContext);
}
}
}
}
throw new NotAcceptableStatusException(mediaTypes);
}
Aggregations