當前位置: 首頁>>代碼示例>>Java>>正文


Java HttpMessageConverter.canWrite方法代碼示例

本文整理匯總了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()
            + "]");
}
 
開發者ID:xm-online,項目名稱:xm-ms-entity,代碼行數:22,代碼來源:MultipartMixedConverter.java

示例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);
    }
}
 
開發者ID:zhaoqilong3031,項目名稱:spring-cloud-samples,代碼行數:36,代碼來源:CustomEncode.java


注:本文中的org.springframework.http.converter.HttpMessageConverter.canWrite方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。