throw new HttpMediaTypeNotSupportedException(ex.getMessage()); throw new HttpMediaTypeNotSupportedException(contentType, new ArrayList<>(mediaTypes));
@Test public void handleHttpMediaTypeNotSupported() { List<MediaType> acceptable = Arrays.asList(MediaType.APPLICATION_ATOM_XML, MediaType.APPLICATION_XML); Exception ex = new HttpMediaTypeNotSupportedException(MediaType.APPLICATION_JSON, acceptable); ResponseEntity<Object> responseEntity = testException(ex); assertEquals(acceptable, responseEntity.getHeaders().getAccept()); }
throw new HttpMediaTypeNotSupportedException(ex.getMessage()); throw new HttpMediaTypeNotSupportedException(contentType, new ArrayList<>(mediaTypes));
throw new HttpMediaTypeNotSupportedException(ex.getMessage()); return null; throw new HttpMediaTypeNotSupportedException(contentType, this.allSupportedMediaTypes);
@Test public void handleHttpMediaTypeNotSupported() { HttpMediaTypeNotSupportedException ex = new HttpMediaTypeNotSupportedException(new MediaType("text", "plain"), Collections.singletonList(new MediaType("application", "pdf"))); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); assertNotNull("No ModelAndView returned", mav); assertTrue("No Empty ModelAndView returned", mav.isEmpty()); assertEquals("Invalid status code", 415, response.getStatus()); assertEquals("Invalid Accept header", "application/pdf", response.getHeader("Accept")); }
throw new HttpMediaTypeNotSupportedException(ex.getMessage()); return null; throw new HttpMediaTypeNotSupportedException(contentType, this.allSupportedMediaTypes);
@Controller public class TestController { @ResponseBody @RequestMapping(value = "/test", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE) public ResponseEntity<ResponseWrapper> test(LoginRequest request) throws HttpMediaTypeNotSupportedException { throw new HttpMediaTypeNotSupportedException(""); } @ExceptionHandler(HttpMediaTypeNotSupportedException.class) @ResponseStatus(value = HttpStatus.UNSUPPORTED_MEDIA_TYPE) public @ResponseBody Map<String, Object> handleUnsupportedMediaTypeException( HttpMediaTypeNotSupportedException ex) throws IOException { Map<String, Object> map = Maps.newHashMap(); map.put("error", "Unsupported Media Type"); map.put("cause", ex.getLocalizedMessage()); map.put("supported", ex.getSupportedMediaTypes()); return map; } }
@SuppressWarnings({ "unchecked", "rawtypes" }) private Object readWithMessageConverters(MethodParameter methodParam, HttpInputMessage inputMessage, Class<?> paramType) throws Exception { MediaType contentType = inputMessage.getHeaders().getContentType(); if (contentType == null) { StringBuilder builder = new StringBuilder(ClassUtils.getShortName(methodParam.getParameterType())); String paramName = methodParam.getParameterName(); if (paramName != null) { builder.append(' '); builder.append(paramName); } throw new HttpMediaTypeNotSupportedException( "Cannot extract parameter (" + builder.toString() + "): no Content-Type found"); } List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>(); if (this.messageConverters != null) { for (HttpMessageConverter<?> messageConverter : this.messageConverters) { allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes()); if (messageConverter.canRead(paramType, contentType)) { if (logger.isDebugEnabled()) { logger.debug("Reading [" + paramType.getName() + "] as \"" + contentType +"\" using [" + messageConverter + "]"); } return messageConverter.read((Class) paramType, inputMessage); } } } throw new HttpMediaTypeNotSupportedException(contentType, allSupportedMediaTypes); }
throw new HttpMediaTypeNotSupportedException(ex.getMessage()); throw new HttpMediaTypeNotSupportedException(contentType, new ArrayList<MediaType>(mediaTypes));
throw new HttpMediaTypeNotSupportedException(ex.getMessage());