本文整理匯總了Java中org.springframework.http.converter.HttpMessageConverter.write方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpMessageConverter.write方法的具體用法?Java HttpMessageConverter.write怎麽用?Java HttpMessageConverter.write使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.http.converter.HttpMessageConverter
的用法示例。
在下文中一共展示了HttpMessageConverter.write方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: encode
import org.springframework.http.converter.HttpMessageConverter; //導入方法依賴的package包/類
@Override
public void encode(Object requestBody, Type bodyType, RequestTemplate request) throws EncodeException {
if (requestBody != null) {
Class<?> requestType = requestBody.getClass();
Collection<String> contentTypes = request.headers().get(HttpHeaders.CONTENT_TYPE);
MediaType requestContentType = null;
if (contentTypes != null && !contentTypes.isEmpty()) {
String type = contentTypes.iterator().next();
requestContentType = MediaType.valueOf(type);
}
for (HttpMessageConverter<?> messageConverter : this.messageConverters.getObject().getConverters()) {
if (messageConverter.canWrite(requestType, requestContentType)) {
FeignOutputMessage outputMessage = new FeignOutputMessage(request);
try {
@SuppressWarnings("unchecked")
HttpMessageConverter<Object> copy = (HttpMessageConverter<Object>) messageConverter;
copy.write(requestBody, requestContentType, outputMessage);
} catch (IOException ex) {
throw new EncodeException("Error converting request body", ex);
}
request.headers(null);
request.headers(FeignUtils.getHeaders(outputMessage.getHeaders()));
request.body(outputMessage.getOutputStream().toByteArray(), Charset.forName("UTF-8")); // TODO:
return;
}
}
String message = "Could not write request: no suitable HttpMessageConverter " + "found for request type ["
+ requestType.getName() + "]";
if (requestContentType != null) {
message += " and content type [" + requestContentType + "]";
}
throw new EncodeException(message);
}
}