@Test public void exceptionInResolvingArg() { this.resolvers.add(stubResolver(Mono.error(new UnsupportedMediaTypeStatusException("boo")))); Method method = ResolvableMethod.on(TestController.class).mockCall(o -> o.singleArg(null)).method(); Mono<HandlerResult> mono = invoke(new TestController(), method); try { mono.block(); fail("Expected UnsupportedMediaTypeStatusException"); } catch (UnsupportedMediaTypeStatusException ex) { assertThat(ex.getMessage(), is("415 UNSUPPORTED_MEDIA_TYPE \"boo\"")); } }
/** * Constructor for when trying to encode from or decode to a specific Java type. * @since 5.1 */ public UnsupportedMediaTypeStatusException(@Nullable MediaType contentType, List<MediaType> supportedTypes, @Nullable ResolvableType bodyType) { super(HttpStatus.UNSUPPORTED_MEDIA_TYPE, initReason(contentType, bodyType)); this.contentType = contentType; this.supportedMediaTypes = Collections.unmodifiableList(supportedTypes); this.bodyType = bodyType; }
@Override protected boolean matchMediaType(ServerWebExchange exchange) throws UnsupportedMediaTypeStatusException { try { MediaType contentType = exchange.getRequest().getHeaders().getContentType(); contentType = (contentType != null ? contentType : MediaType.APPLICATION_OCTET_STREAM); return getMediaType().includes(contentType); } catch (InvalidMediaTypeException ex) { throw new UnsupportedMediaTypeStatusException("Can't parse Content-Type [" + exchange.getRequest().getHeaders().getFirst("Content-Type") + "]: " + ex.getMessage()); } } }
private void testHttpMediaTypeNotSupportedException(String url) { MockServerHttpRequest request = put(url).contentType(MediaType.APPLICATION_JSON).build(); ServerWebExchange exchange = MockServerWebExchange.from(request); Mono<Object> mono = this.handlerMapping.getHandler(exchange); assertError(mono, UnsupportedMediaTypeStatusException.class, ex -> assertEquals("Invalid supported consumable media types", Collections.singletonList(new MediaType("application", "xml")), ex.getSupportedMediaTypes())); }
@Test public void getHandlerTestInvalidContentType() { MockServerHttpRequest request = put("/person/1").header("content-type", "bogus").build(); ServerWebExchange exchange = MockServerWebExchange.from(request); Mono<Object> mono = this.handlerMapping.getHandler(exchange); assertError(mono, UnsupportedMediaTypeStatusException.class, ex -> assertEquals("415 UNSUPPORTED_MEDIA_TYPE " + "\"Invalid mime type \"bogus\": does not contain '/'\"", ex.getMessage())); }
throw new UnsupportedMediaTypeStatusException(ex.getMessage()); throw new UnsupportedMediaTypeStatusException(contentType, new ArrayList<>(mediaTypes));
@API(status = INTERNAL) @ExceptionHandler default Mono<ResponseEntity<Problem>> handleMediaTypeNotSupportedException( final UnsupportedMediaTypeStatusException exception, final ServerWebExchange request) { final HttpHeaders headers = new HttpHeaders(); headers.setAccept(exception.getSupportedMediaTypes()); return create(Status.UNSUPPORTED_MEDIA_TYPE, exception, request, headers); }
Flux<DataBuffer> body = request.getBody().doOnNext(o -> { throw new UnsupportedMediaTypeStatusException(mediaType, this.supportedMediaTypes, elementType); }); if (isBodyRequired) { return Mono.error(new UnsupportedMediaTypeStatusException(mediaType, this.supportedMediaTypes, elementType));
/** * Constructor for when trying to encode from or decode to a specific Java type. * @since 5.1 */ public UnsupportedMediaTypeStatusException(@Nullable MediaType contentType, List<MediaType> supportedTypes, @Nullable ResolvableType bodyType) { super(HttpStatus.UNSUPPORTED_MEDIA_TYPE, initReason(contentType, bodyType)); this.contentType = contentType; this.supportedMediaTypes = Collections.unmodifiableList(supportedTypes); this.bodyType = bodyType; }
.filter(reader -> reader.canRead(elementType, contentType)) .findFirst() .orElseThrow(() -> new UnsupportedMediaTypeStatusException( "Could not convert request: no suitable HttpMessageReader found for expected type [" + elementType + "] and content type [" + contentType + "]"));
/** * Constructor for when trying to encode from or decode to a specific Java type. * @since 5.1 */ public UnsupportedMediaTypeStatusException(@Nullable MediaType contentType, List<MediaType> supportedTypes, @Nullable ResolvableType bodyType) { super(HttpStatus.UNSUPPORTED_MEDIA_TYPE, initReason(contentType, bodyType)); this.contentType = contentType; this.supportedMediaTypes = Collections.unmodifiableList(supportedTypes); this.bodyType = bodyType; }
@Override protected boolean matchMediaType(ServerWebExchange exchange) throws UnsupportedMediaTypeStatusException { try { MediaType contentType = exchange.getRequest().getHeaders().getContentType(); contentType = (contentType != null ? contentType : MediaType.APPLICATION_OCTET_STREAM); return getMediaType().includes(contentType); } catch (InvalidMediaTypeException ex) { throw new UnsupportedMediaTypeStatusException("Can't parse Content-Type [" + exchange.getRequest().getHeaders().getFirst("Content-Type") + "]: " + ex.getMessage()); } } }
throw new UnsupportedMediaTypeStatusException(ex.getMessage()); throw new UnsupportedMediaTypeStatusException(contentType, new ArrayList<>(mediaTypes));
return Mono.error(new UnsupportedMediaTypeStatusException(mediaType, Arrays.asList(MediaType.APPLICATION_JSON), elementType));
return Mono.error(new UnsupportedMediaTypeStatusException(mediaType, Arrays.asList(MediaType.APPLICATION_JSON), elementType));
return Mono.error(new UnsupportedMediaTypeStatusException(mediaType, this.supportedMediaTypes));