Java Code Examples for org.springframework.http.converter.HttpMessageConverter#canRead()
The following examples show how to use
org.springframework.http.converter.HttpMessageConverter#canRead() .
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: DefaultServerRequestBuilder.java From spring-analysis-note with MIT License | 6 votes |
@SuppressWarnings("unchecked") private <T> T bodyInternal(Type bodyType, Class<?> bodyClass) throws ServletException, IOException { HttpInputMessage inputMessage = new BuiltInputMessage(); MediaType contentType = headers().contentType().orElse(MediaType.APPLICATION_OCTET_STREAM); for (HttpMessageConverter<?> messageConverter : this.messageConverters) { if (messageConverter instanceof GenericHttpMessageConverter) { GenericHttpMessageConverter<T> genericMessageConverter = (GenericHttpMessageConverter<T>) messageConverter; if (genericMessageConverter.canRead(bodyType, bodyClass, contentType)) { return genericMessageConverter.read(bodyType, bodyClass, inputMessage); } } if (messageConverter.canRead(bodyClass, contentType)) { HttpMessageConverter<T> theConverter = (HttpMessageConverter<T>) messageConverter; Class<? extends T> clazz = (Class<? extends T>) bodyClass; return theConverter.read(clazz, inputMessage); } } throw new HttpMediaTypeNotSupportedException(contentType, Collections.emptyList()); }
Example 2
Source File: DefaultServerRequest.java From spring-analysis-note with MIT License | 6 votes |
@SuppressWarnings("unchecked") private <T> T bodyInternal(Type bodyType, Class<?> bodyClass) throws ServletException, IOException { MediaType contentType = this.headers.contentType().orElse(MediaType.APPLICATION_OCTET_STREAM); for (HttpMessageConverter<?> messageConverter : this.messageConverters) { if (messageConverter instanceof GenericHttpMessageConverter) { GenericHttpMessageConverter<T> genericMessageConverter = (GenericHttpMessageConverter<T>) messageConverter; if (genericMessageConverter.canRead(bodyType, bodyClass, contentType)) { return genericMessageConverter.read(bodyType, bodyClass, this.serverHttpRequest); } } if (messageConverter.canRead(bodyClass, contentType)) { HttpMessageConverter<T> theConverter = (HttpMessageConverter<T>) messageConverter; Class<? extends T> clazz = (Class<? extends T>) bodyClass; return theConverter.read(clazz, this.serverHttpRequest); } } throw new HttpMediaTypeNotSupportedException(contentType, this.allSupportedMediaTypes); }
Example 3
Source File: DCTMJaxbErrorHandler.java From documentum-rest-client-java with Apache License 2.0 | 6 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public void handleError(ClientHttpResponse response) throws IOException { MediaType mediaType = response.getHeaders().getContentType(); RestError error = null; for(HttpMessageConverter converter : converters) { if(converter.canRead(JaxbRestError.class, mediaType)) { try { error = (RestError)converter.read(JaxbRestError.class, response); } catch(Exception e) { error = new JaxbRestError(); ((JaxbRestError)error).setStatus(response.getRawStatusCode()); } break; } } throw new DCTMRestErrorException(response.getHeaders(), response.getStatusCode(), error); }
Example 4
Source File: WxApiMessageConverterExtractor.java From FastBootWeixin with Apache License 2.0 | 6 votes |
@Override public T extractData(ResponseEntity<HttpInputMessage> responseEntity) throws IOException { MediaType contentType = getContentType(responseEntity); for (HttpMessageConverter<?> messageConverter : this.messageConverters) { if (messageConverter instanceof GenericHttpMessageConverter) { GenericHttpMessageConverter<?> genericMessageConverter = (GenericHttpMessageConverter<?>) messageConverter; if (genericMessageConverter.canRead(this.responseType, null, contentType)) { return (T) genericMessageConverter.read(this.responseType, null, responseEntity.getBody()); } } if (this.responseClass != null) { if (messageConverter.canRead(this.responseClass, contentType)) { return (T) messageConverter.read((Class) this.responseClass, responseEntity.getBody()); } } } throw new WxApiResponseException("不能转换相应数据为类型:" + this.responseType, responseEntity); }
Example 5
Source File: NotificationMessageHandlerMethodArgumentResolver.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) protected Object doResolveArgumentFromNotificationMessage(JsonNode content, HttpInputMessage request, Class<?> parameterType) { if (!"Notification".equals(content.get("Type").asText())) { throw new IllegalArgumentException( "@NotificationMessage annotated parameters are only allowed for method that receive a notification message."); } MediaType mediaType = getMediaType(content); String messageContent = content.findPath("Message").asText(); for (HttpMessageConverter<?> converter : this.messageConverter) { if (converter.canRead(parameterType, mediaType)) { try { return converter.read((Class) parameterType, new ByteArrayHttpInputMessage(messageContent, mediaType, request)); } catch (Exception e) { throw new HttpMessageNotReadableException( "Error converting notification message with payload:" + messageContent, e); } } } throw new HttpMessageNotReadableException( "Error converting notification message with payload:" + messageContent); }
Example 6
Source File: SpringConverterFactory.java From spring-cloud-square with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") @Override public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { if (type instanceof Class || type instanceof ParameterizedType) { //MediaType contentType = getContentType(responseWrapper); MediaType contentType = MediaType.APPLICATION_JSON; //TODO: determine dynamically? Class<?> responseClass = (type instanceof Class) ? (Class<?>) type : null; for (HttpMessageConverter<?> messageConverter : this.messageConverters.getObject().getConverters()) { if (messageConverter instanceof GenericHttpMessageConverter) { GenericHttpMessageConverter<?> genericMessageConverter = (GenericHttpMessageConverter<?>) messageConverter; if (genericMessageConverter.canRead(type, null, contentType)) { if (log.isDebugEnabled()) { log.debug("Reading [" + type + "] as \"" + contentType + "\" using [" + messageConverter + "]"); } return new SpringResponseConverter(genericMessageConverter, type); } } if (responseClass != null) { if (messageConverter.canRead(responseClass, contentType)) { if (log.isDebugEnabled()) { log.debug("Reading [" + responseClass.getName() + "] as \"" + contentType + "\" using [" + messageConverter + "]"); } return new SpringResponseConverter(messageConverter, responseClass); } } } } return null; }
Example 7
Source File: DCTMJacksonErrorHandler.java From documentum-rest-client-java with Apache License 2.0 | 5 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public void handleError(ClientHttpResponse response) throws IOException { MediaType mediaType = response.getHeaders().getContentType(); RestError error = null; for(HttpMessageConverter converter : converters) { if(converter.canRead(JsonRestError.class, mediaType)) { error = (RestError)converter.read(JsonRestError.class, response); break; } } throw new DCTMRestErrorException(response.getHeaders(), response.getStatusCode(), error); }
Example 8
Source File: HandlerMethodInvoker.java From spring4-understanding with Apache License 2.0 | 5 votes |
@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); }
Example 9
Source File: HttpMessageConverterExtractor.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override @SuppressWarnings({"unchecked", "rawtypes"}) public T extractData(ClientHttpResponse response) throws IOException { MessageBodyClientHttpResponseWrapper responseWrapper = new MessageBodyClientHttpResponseWrapper(response); if (!responseWrapper.hasMessageBody() || responseWrapper.hasEmptyMessageBody()) { return null; } MediaType contentType = getContentType(responseWrapper); for (HttpMessageConverter<?> messageConverter : this.messageConverters) { if (messageConverter instanceof GenericHttpMessageConverter) { GenericHttpMessageConverter<?> genericMessageConverter = (GenericHttpMessageConverter<?>) messageConverter; if (genericMessageConverter.canRead(this.responseType, null, contentType)) { if (logger.isDebugEnabled()) { logger.debug("Reading [" + this.responseType + "] as \"" + contentType + "\" using [" + messageConverter + "]"); } return (T) genericMessageConverter.read(this.responseType, null, responseWrapper); } } if (this.responseClass != null) { if (messageConverter.canRead(this.responseClass, contentType)) { if (logger.isDebugEnabled()) { logger.debug("Reading [" + this.responseClass.getName() + "] as \"" + contentType + "\" using [" + messageConverter + "]"); } return (T) messageConverter.read((Class) this.responseClass, responseWrapper); } } } throw new RestClientException("Could not extract response: no suitable HttpMessageConverter found " + "for response type [" + this.responseType + "] and content type [" + contentType + "]"); }
Example 10
Source File: RestTemplate.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override public void doWithRequest(ClientHttpRequest request) throws IOException { if (this.responseType != null) { Class<?> responseClass = null; if (this.responseType instanceof Class) { responseClass = (Class<?>) this.responseType; } List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>(); for (HttpMessageConverter<?> converter : getMessageConverters()) { if (responseClass != null) { if (converter.canRead(responseClass, null)) { allSupportedMediaTypes.addAll(getSupportedMediaTypes(converter)); } } else if (converter instanceof GenericHttpMessageConverter) { GenericHttpMessageConverter<?> genericConverter = (GenericHttpMessageConverter<?>) converter; if (genericConverter.canRead(this.responseType, null, null)) { allSupportedMediaTypes.addAll(getSupportedMediaTypes(converter)); } } } if (!allSupportedMediaTypes.isEmpty()) { MediaType.sortBySpecificity(allSupportedMediaTypes); if (logger.isDebugEnabled()) { logger.debug("Setting request Accept header to " + allSupportedMediaTypes); } request.getHeaders().setAccept(allSupportedMediaTypes); } } }
Example 11
Source File: HandlerMethodInvoker.java From lams with GNU General Public License v2.0 | 5 votes |
@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); }
Example 12
Source File: HttpMessageConverterExtractor.java From lams with GNU General Public License v2.0 | 5 votes |
@Override @SuppressWarnings({"unchecked", "rawtypes", "resource"}) public T extractData(ClientHttpResponse response) throws IOException { MessageBodyClientHttpResponseWrapper responseWrapper = new MessageBodyClientHttpResponseWrapper(response); if (!responseWrapper.hasMessageBody() || responseWrapper.hasEmptyMessageBody()) { return null; } MediaType contentType = getContentType(responseWrapper); for (HttpMessageConverter<?> messageConverter : this.messageConverters) { if (messageConverter instanceof GenericHttpMessageConverter) { GenericHttpMessageConverter<?> genericMessageConverter = (GenericHttpMessageConverter<?>) messageConverter; if (genericMessageConverter.canRead(this.responseType, null, contentType)) { if (logger.isDebugEnabled()) { logger.debug("Reading [" + this.responseType + "] as \"" + contentType + "\" using [" + messageConverter + "]"); } return (T) genericMessageConverter.read(this.responseType, null, responseWrapper); } } if (this.responseClass != null) { if (messageConverter.canRead(this.responseClass, contentType)) { if (logger.isDebugEnabled()) { logger.debug("Reading [" + this.responseClass.getName() + "] as \"" + contentType + "\" using [" + messageConverter + "]"); } return (T) messageConverter.read((Class) this.responseClass, responseWrapper); } } } throw new RestClientException("Could not extract response: no suitable HttpMessageConverter found " + "for response type [" + this.responseType + "] and content type [" + contentType + "]"); }
Example 13
Source File: RestTemplate.java From lams with GNU General Public License v2.0 | 5 votes |
@Override public void doWithRequest(ClientHttpRequest request) throws IOException { if (this.responseType != null) { Class<?> responseClass = null; if (this.responseType instanceof Class) { responseClass = (Class<?>) this.responseType; } List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>(); for (HttpMessageConverter<?> converter : getMessageConverters()) { if (responseClass != null) { if (converter.canRead(responseClass, null)) { allSupportedMediaTypes.addAll(getSupportedMediaTypes(converter)); } } else if (converter instanceof GenericHttpMessageConverter) { GenericHttpMessageConverter<?> genericConverter = (GenericHttpMessageConverter<?>) converter; if (genericConverter.canRead(this.responseType, null, null)) { allSupportedMediaTypes.addAll(getSupportedMediaTypes(converter)); } } } if (!allSupportedMediaTypes.isEmpty()) { MediaType.sortBySpecificity(allSupportedMediaTypes); if (logger.isDebugEnabled()) { logger.debug("Setting request Accept header to " + allSupportedMediaTypes); } request.getHeaders().setAccept(allSupportedMediaTypes); } } }
Example 14
Source File: HttpMessageConverterResolver.java From spring-cloud-alibaba with Apache License 2.0 | 5 votes |
public HttpMessageConverterHolder resolve(HttpRequest request, Class<?> parameterType) { HttpMessageConverterHolder httpMessageConverterHolder = null; HttpHeaders httpHeaders = request.getHeaders(); MediaType contentType = httpHeaders.getContentType(); if (contentType == null) { contentType = MediaType.APPLICATION_OCTET_STREAM; } for (HttpMessageConverter<?> converter : this.messageConverters) { if (converter instanceof GenericHttpMessageConverter) { GenericHttpMessageConverter genericConverter = (GenericHttpMessageConverter) converter; if (genericConverter.canRead(parameterType, parameterType, contentType)) { httpMessageConverterHolder = new HttpMessageConverterHolder( contentType, converter); break; } } else { if (converter.canRead(parameterType, contentType)) { httpMessageConverterHolder = new HttpMessageConverterHolder( contentType, converter); break; } } } return httpMessageConverterHolder; }
Example 15
Source File: RestTemplate.java From java-technology-stack with MIT License | 5 votes |
private boolean canReadResponse(Type responseType, HttpMessageConverter<?> converter) { Class<?> responseClass = (responseType instanceof Class ? (Class<?>) responseType : null); if (responseClass != null) { return converter.canRead(responseClass, null); } else if (converter instanceof GenericHttpMessageConverter) { GenericHttpMessageConverter<?> genericConverter = (GenericHttpMessageConverter<?>) converter; return genericConverter.canRead(responseType, null, null); } return false; }
Example 16
Source File: RestTemplate.java From spring-analysis-note with MIT License | 5 votes |
private boolean canReadResponse(Type responseType, HttpMessageConverter<?> converter) { Class<?> responseClass = (responseType instanceof Class ? (Class<?>) responseType : null); if (responseClass != null) { return converter.canRead(responseClass, null); } else if (converter instanceof GenericHttpMessageConverter) { GenericHttpMessageConverter<?> genericConverter = (GenericHttpMessageConverter<?>) converter; return genericConverter.canRead(responseType, null, null); } return false; }
Example 17
Source File: HttpMessageConverterExtractor.java From java-technology-stack with MIT License | 4 votes |
@Override @SuppressWarnings({"unchecked", "rawtypes", "resource"}) public T extractData(ClientHttpResponse response) throws IOException { MessageBodyClientHttpResponseWrapper responseWrapper = new MessageBodyClientHttpResponseWrapper(response); if (!responseWrapper.hasMessageBody() || responseWrapper.hasEmptyMessageBody()) { return null; } MediaType contentType = getContentType(responseWrapper); try { for (HttpMessageConverter<?> messageConverter : this.messageConverters) { if (messageConverter instanceof GenericHttpMessageConverter) { GenericHttpMessageConverter<?> genericMessageConverter = (GenericHttpMessageConverter<?>) messageConverter; if (genericMessageConverter.canRead(this.responseType, null, contentType)) { if (logger.isDebugEnabled()) { ResolvableType resolvableType = ResolvableType.forType(this.responseType); logger.debug("Reading to [" + resolvableType + "]"); } return (T) genericMessageConverter.read(this.responseType, null, responseWrapper); } } if (this.responseClass != null) { if (messageConverter.canRead(this.responseClass, contentType)) { if (logger.isDebugEnabled()) { String className = this.responseClass.getName(); logger.debug("Reading to [" + className + "] as \"" + contentType + "\""); } return (T) messageConverter.read((Class) this.responseClass, responseWrapper); } } } } catch (IOException | HttpMessageNotReadableException ex) { throw new RestClientException("Error while extracting response for type [" + this.responseType + "] and content type [" + contentType + "]", ex); } throw new RestClientException("Could not extract response: no suitable HttpMessageConverter found " + "for response type [" + this.responseType + "] and content type [" + contentType + "]"); }
Example 18
Source File: HttpMessageConverterExtractor.java From spring-analysis-note with MIT License | 4 votes |
@Override @SuppressWarnings({"unchecked", "rawtypes", "resource"}) public T extractData(ClientHttpResponse response) throws IOException { MessageBodyClientHttpResponseWrapper responseWrapper = new MessageBodyClientHttpResponseWrapper(response); if (!responseWrapper.hasMessageBody() || responseWrapper.hasEmptyMessageBody()) { return null; } MediaType contentType = getContentType(responseWrapper); try { for (HttpMessageConverter<?> messageConverter : this.messageConverters) { if (messageConverter instanceof GenericHttpMessageConverter) { GenericHttpMessageConverter<?> genericMessageConverter = (GenericHttpMessageConverter<?>) messageConverter; if (genericMessageConverter.canRead(this.responseType, null, contentType)) { if (logger.isDebugEnabled()) { ResolvableType resolvableType = ResolvableType.forType(this.responseType); logger.debug("Reading to [" + resolvableType + "]"); } return (T) genericMessageConverter.read(this.responseType, null, responseWrapper); } } if (this.responseClass != null) { if (messageConverter.canRead(this.responseClass, contentType)) { if (logger.isDebugEnabled()) { String className = this.responseClass.getName(); logger.debug("Reading to [" + className + "] as \"" + contentType + "\""); } return (T) messageConverter.read((Class) this.responseClass, responseWrapper); } } } } catch (IOException | HttpMessageNotReadableException ex) { throw new RestClientException("Error while extracting response for type [" + this.responseType + "] and content type [" + contentType + "]", ex); } throw new RestClientException("Could not extract response: no suitable HttpMessageConverter found " + "for response type [" + this.responseType + "] and content type [" + contentType + "]"); }