Java Code Examples for org.springframework.http.converter.GenericHttpMessageConverter#canRead()
The following examples show how to use
org.springframework.http.converter.GenericHttpMessageConverter#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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
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 + "]"); }
Example 13
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 + "]"); }