@Override protected OAuth2AccessTokenResponse readInternal(Class<? extends OAuth2AccessTokenResponse> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { try { @SuppressWarnings("unchecked") Map<String, String> tokenResponseParameters = (Map<String, String>) this.jsonMessageConverter.read( PARAMETERIZED_RESPONSE_TYPE.getType(), null, inputMessage); return this.tokenResponseConverter.convert(tokenResponseParameters); } catch (Exception ex) { throw new HttpMessageNotReadableException("An error occurred reading the OAuth 2.0 Access Token Response: " + ex.getMessage(), ex, inputMessage); } }
@Override protected OAuth2Error readInternal(Class<? extends OAuth2Error> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { try { @SuppressWarnings("unchecked") Map<String, String> errorParameters = (Map<String, String>) this.jsonMessageConverter.read( PARAMETERIZED_RESPONSE_TYPE.getType(), null, inputMessage); return this.errorConverter.convert(errorParameters); } catch (Exception ex) { throw new HttpMessageNotReadableException("An error occurred reading the OAuth 2.0 Error: " + ex.getMessage(), ex, inputMessage); } }
logger.debug("Reading to [" + resolvableType + "]"); return (T) genericMessageConverter.read(this.responseType, null, responseWrapper);
logger.debug("Reading to [" + resolvableType + "]"); return (T) genericMessageConverter.read(this.responseType, null, responseWrapper);
given(response.getBody()).willReturn(new ByteArrayInputStream(Integer.toString(42).getBytes())); given(converter.canRead(intList.getType(), null, MediaType.TEXT_PLAIN)).willReturn(true); given(converter.read(eq(intList.getType()), eq(null), any(HttpInputMessage.class))).willReturn(expected);
HttpInputMessage msgToUse = getAdvice().beforeBodyRead(message, parameter, targetType, converterType); body = (genericConverter != null ? genericConverter.read(targetType, contextClass, msgToUse) : ((HttpMessageConverter<T>) converter).read(targetClass, msgToUse)); body = getAdvice().afterBodyRead(body, msgToUse, parameter, targetType, converterType);
@Test @SuppressWarnings("unchecked") public void generics() throws IOException { GenericHttpMessageConverter<String> converter = mock(GenericHttpMessageConverter.class); HttpHeaders responseHeaders = new HttpHeaders(); MediaType contentType = MediaType.TEXT_PLAIN; responseHeaders.setContentType(contentType); String expected = "Foo"; ParameterizedTypeReference<List<String>> reference = new ParameterizedTypeReference<List<String>>() {}; Type type = reference.getType(); extractor = new HttpMessageConverterExtractor<List<String>>(type, createConverterList(converter)); given(response.getRawStatusCode()).willReturn(HttpStatus.OK.value()); given(response.getHeaders()).willReturn(responseHeaders); given(response.getBody()).willReturn(new ByteArrayInputStream(expected.getBytes())); given(converter.canRead(type, null, contentType)).willReturn(true); given(converter.read(eq(type), eq(null), any(HttpInputMessage.class))).willReturn(expected); Object result = extractor.extractData(response); assertEquals(expected, result); }
HttpInputMessage msgToUse = getAdvice().beforeBodyRead(message, parameter, targetType, converterType); body = (genericConverter != null ? genericConverter.read(targetType, contextClass, msgToUse) : ((HttpMessageConverter<T>) converter).read(targetClass, msgToUse)); body = getAdvice().afterBodyRead(body, msgToUse, parameter, targetType, converterType);
@Override protected OAuth2AccessTokenResponse readInternal(Class<? extends OAuth2AccessTokenResponse> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { try { @SuppressWarnings("unchecked") Map<String, String> tokenResponseParameters = (Map<String, String>) this.jsonMessageConverter.read( PARAMETERIZED_RESPONSE_TYPE.getType(), null, inputMessage); return this.tokenResponseConverter.convert(tokenResponseParameters); } catch (Exception ex) { throw new HttpMessageNotReadableException("An error occurred reading the OAuth 2.0 Access Token Response: " + ex.getMessage(), ex, inputMessage); } }
@Override protected OAuth2Error readInternal(Class<? extends OAuth2Error> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { try { @SuppressWarnings("unchecked") Map<String, String> errorParameters = (Map<String, String>) this.jsonMessageConverter.read( PARAMETERIZED_RESPONSE_TYPE.getType(), null, inputMessage); return this.errorConverter.convert(errorParameters); } catch (Exception ex) { throw new HttpMessageNotReadableException("An error occurred reading the OAuth 2.0 Error: " + ex.getMessage(), ex, inputMessage); } }
@Override protected OAuth2AccessTokenResponse readInternal(Class<? extends OAuth2AccessTokenResponse> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { try { @SuppressWarnings("unchecked") Map<String, String> tokenResponseParameters = (Map<String, String>) this.jsonMessageConverter.read( PARAMETERIZED_RESPONSE_TYPE.getType(), null, inputMessage); return this.tokenResponseConverter.convert(tokenResponseParameters); } catch (Exception ex) { throw new HttpMessageNotReadableException("An error occurred reading the OAuth 2.0 Access Token Response: " + ex.getMessage(), ex, inputMessage); } }
@Override protected OAuth2Error readInternal(Class<? extends OAuth2Error> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { try { @SuppressWarnings("unchecked") Map<String, String> errorParameters = (Map<String, String>) this.jsonMessageConverter.read( PARAMETERIZED_RESPONSE_TYPE.getType(), null, inputMessage); return this.errorConverter.convert(errorParameters); } catch (Exception ex) { throw new HttpMessageNotReadableException("An error occurred reading the OAuth 2.0 Error: " + ex.getMessage(), ex, inputMessage); } }
@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); }
logger.debug("Reading to [" + resolvableType + "]"); return (T) genericMessageConverter.read(this.responseType, null, responseWrapper);
contentType + "\" using [" + messageConverter + "]"); return (T) genericMessageConverter.read(this.responseType, null, responseWrapper);
body = genericConverter.read(targetType, contextClass, inputMessage); body = getAdvice().afterBodyRead(body, inputMessage, parameter, targetType, converterType);