Java Code Examples for org.springframework.http.converter.HttpMessageConverter#write()
The following examples show how to use
org.springframework.http.converter.HttpMessageConverter#write() .
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: DubboClientHttpResponseFactory.java From spring-cloud-alibaba with Apache License 2.0 | 6 votes |
public ClientHttpResponse build(Object result, GenericException exception, RequestMetadata requestMetadata, RestMethodMetadata restMethodMetadata) { DubboHttpOutputMessage httpOutputMessage = new DubboHttpOutputMessage(); HttpMessageConverterHolder httpMessageConverterHolder = httpMessageConverterResolver .resolve(requestMetadata, restMethodMetadata); if (httpMessageConverterHolder != null) { MediaType mediaType = httpMessageConverterHolder.getMediaType(); HttpMessageConverter converter = httpMessageConverterHolder.getConverter(); try { converter.write(result, mediaType, httpOutputMessage); } catch (IOException e) { e.printStackTrace(); } } return new DubboClientHttpResponse(httpOutputMessage, exception); }
Example 2
Source File: SpringEncoder.java From spring-cloud-openfeign with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") private FeignOutputMessage checkAndWrite(Object body, MediaType contentType, HttpMessageConverter converter, RequestTemplate request) throws IOException { if (converter.canWrite(body.getClass(), contentType)) { logBeforeWrite(body, contentType, converter); FeignOutputMessage outputMessage = new FeignOutputMessage(request); converter.write(body, contentType, outputMessage); return outputMessage; } else { return null; } }
Example 3
Source File: AnnotationMethodHandlerExceptionResolver.java From lams with GNU General Public License v2.0 | 5 votes |
@SuppressWarnings({ "unchecked", "rawtypes", "resource" }) private ModelAndView handleResponseBody(Object returnValue, ServletWebRequest webRequest) throws ServletException, IOException { HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest()); List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept(); if (acceptedMediaTypes.isEmpty()) { acceptedMediaTypes = Collections.singletonList(MediaType.ALL); } MediaType.sortByQualityValue(acceptedMediaTypes); HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse()); Class<?> returnValueType = returnValue.getClass(); if (this.messageConverters != null) { for (MediaType acceptedMediaType : acceptedMediaTypes) { for (HttpMessageConverter messageConverter : this.messageConverters) { if (messageConverter.canWrite(returnValueType, acceptedMediaType)) { messageConverter.write(returnValue, acceptedMediaType, outputMessage); return new ModelAndView(); } } } } if (logger.isWarnEnabled()) { logger.warn("Could not find HttpMessageConverter that supports return type [" + returnValueType + "] and " + acceptedMediaTypes); } return null; }
Example 4
Source File: AnnotationMethodHandlerExceptionResolver.java From spring4-understanding with Apache License 2.0 | 5 votes |
@SuppressWarnings({ "unchecked", "rawtypes", "resource" }) private ModelAndView handleResponseBody(Object returnValue, ServletWebRequest webRequest) throws ServletException, IOException { HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest()); List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept(); if (acceptedMediaTypes.isEmpty()) { acceptedMediaTypes = Collections.singletonList(MediaType.ALL); } MediaType.sortByQualityValue(acceptedMediaTypes); HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse()); Class<?> returnValueType = returnValue.getClass(); if (this.messageConverters != null) { for (MediaType acceptedMediaType : acceptedMediaTypes) { for (HttpMessageConverter messageConverter : this.messageConverters) { if (messageConverter.canWrite(returnValueType, acceptedMediaType)) { messageConverter.write(returnValue, acceptedMediaType, outputMessage); return new ModelAndView(); } } } } if (logger.isWarnEnabled()) { logger.warn("Could not find HttpMessageConverter that supports return type [" + returnValueType + "] and " + acceptedMediaTypes); } return null; }
Example 5
Source File: SpringRequestConverter.java From spring-cloud-square with Apache License 2.0 | 4 votes |
@Override public RequestBody convert(T value) throws IOException { if (value != null) { if (log.isDebugEnabled()) { if (this.contentType != null) { log.debug("Writing [" + value + "] as \"" + this.contentType + "\" using [" + messageConverter + "]"); } else { log.debug("Writing [" + value + "] using [" + messageConverter + "]"); } } return new RequestBody() { @Override public okhttp3.MediaType contentType() { return okhttp3.MediaType.parse(SpringRequestConverter.this.contentType.toString()); } @Override public void writeTo(BufferedSink bufferedSink) throws IOException { @SuppressWarnings("unchecked") HttpMessageConverter<Object> copy = (HttpMessageConverter<Object>) messageConverter; copy.write(value, SpringRequestConverter.this.contentType, new HttpOutputMessage() { @Override public OutputStream getBody() throws IOException { return bufferedSink.outputStream(); } @Override public HttpHeaders getHeaders() { return new HttpHeaders(); // TODO: where to get headers? } }); } }; /* * FeignOutputMessage outputMessage = new FeignOutputMessage(request); try { * * @SuppressWarnings("unchecked") HttpMessageConverter<Object> copy = * (HttpMessageConverter<Object>) messageConverter; copy.write(requestBody, * this.contentType, outputMessage); } catch (IOException ex) { throw new * EncodeException("Error converting request body", ex); } // clear headers * request.headers(null); // converters can modify headers, so update the * request // with the modified headers * request.headers(getHeaders(outputMessage.getHeaders())); * * request.body(outputMessage.getOutputStream().toByteArray(), * Charset.forName("UTF-8")); // TODO: set charset return; */ } return null; }