本文整理匯總了Java中org.springframework.http.converter.HttpMessageConverter.canWrite方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpMessageConverter.canWrite方法的具體用法?Java HttpMessageConverter.canWrite怎麽用?Java HttpMessageConverter.canWrite使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.http.converter.HttpMessageConverter
的用法示例。
在下文中一共展示了HttpMessageConverter.canWrite方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: writePart
import org.springframework.http.converter.HttpMessageConverter; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
private void writePart(String name, HttpEntity<?> partEntity, OutputStream os) throws IOException {
Object partBody = partEntity.getBody();
Class<?> partType = partBody.getClass();
HttpHeaders partHeaders = partEntity.getHeaders();
MediaType partContentType = partHeaders.getContentType();
for (HttpMessageConverter<?> messageConverter : this.partConverters) {
if (messageConverter.canWrite(partType, partContentType)) {
HttpOutputMessage multipartMessage = new MultipartMixedConverter.MultipartHttpOutputMessage(os);
multipartMessage.getHeaders().setContentDispositionFormData(name, null);
if (!partHeaders.isEmpty()) {
multipartMessage.getHeaders().putAll(partHeaders);
}
((HttpMessageConverter<Object>) messageConverter).write(partBody, partContentType, multipartMessage);
return;
}
}
throw new HttpMessageNotWritableException(
"Could not write request: no suitable HttpMessageConverter found for request type [" + partType.getName()
+ "]");
}
示例2: 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);
}
}