Java Code Examples for org.springframework.http.converter.GenericHttpMessageConverter#write()
The following examples show how to use
org.springframework.http.converter.GenericHttpMessageConverter#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: DefaultEntityResponseBuilder.java From spring-analysis-note with MIT License | 5 votes |
@SuppressWarnings("unchecked") protected void writeEntityWithMessageConverters(Object entity, HttpServletRequest request, HttpServletResponse response, ServerResponse.Context context) throws ServletException, IOException { ServletServerHttpResponse serverResponse = new ServletServerHttpResponse(response); MediaType contentType = getContentType(response); Class<?> entityClass = entity.getClass(); for (HttpMessageConverter<?> messageConverter : context.messageConverters()) { if (messageConverter instanceof GenericHttpMessageConverter<?>) { GenericHttpMessageConverter<Object> genericMessageConverter = (GenericHttpMessageConverter<Object>) messageConverter; if (genericMessageConverter.canWrite(this.entityType, entityClass, contentType)) { genericMessageConverter.write(entity, this.entityType, contentType, serverResponse); return; } } if (messageConverter.canWrite(entityClass, contentType)) { ((HttpMessageConverter<Object>)messageConverter).write(entity, contentType, serverResponse); return; } } List<MediaType> producibleMediaTypes = producibleMediaTypes(context.messageConverters(), entityClass); throw new HttpMediaTypeNotAcceptableException(producibleMediaTypes); }
Example 2
Source File: SpringEncoder.java From spring-cloud-openfeign with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") private FeignOutputMessage checkAndWrite(Object body, Type genericType, MediaType contentType, GenericHttpMessageConverter converter, RequestTemplate request) throws IOException { if (converter.canWrite(genericType, body.getClass(), contentType)) { logBeforeWrite(body, contentType, converter); FeignOutputMessage outputMessage = new FeignOutputMessage(request); converter.write(body, genericType, contentType, outputMessage); return outputMessage; } else { return null; } }
Example 3
Source File: RestTemplateTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("rawtypes") public void exchangeParameterizedType() throws Exception { GenericHttpMessageConverter converter = mock(GenericHttpMessageConverter.class); template.setMessageConverters(Collections.<HttpMessageConverter<?>>singletonList(converter)); ParameterizedTypeReference<List<Integer>> intList = new ParameterizedTypeReference<List<Integer>>() {}; given(converter.canRead(intList.getType(), null, null)).willReturn(true); given(converter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN)); given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.POST)).willReturn(this.request); HttpHeaders requestHeaders = new HttpHeaders(); given(this.request.getHeaders()).willReturn(requestHeaders); given(converter.canWrite(String.class, null)).willReturn(true); String requestBody = "Hello World"; converter.write(requestBody, null, this.request); given(this.request.execute()).willReturn(response); given(errorHandler.hasError(response)).willReturn(false); List<Integer> expected = Collections.singletonList(42); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentType(MediaType.TEXT_PLAIN); responseHeaders.setContentLength(10); given(response.getStatusCode()).willReturn(HttpStatus.OK); given(response.getHeaders()).willReturn(responseHeaders); given(response.getBody()).willReturn(new ByteArrayInputStream(new Integer(42).toString().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); given(response.getStatusCode()).willReturn(HttpStatus.OK); HttpStatus status = HttpStatus.OK; given(response.getStatusCode()).willReturn(status); given(response.getStatusText()).willReturn(status.getReasonPhrase()); HttpHeaders entityHeaders = new HttpHeaders(); entityHeaders.set("MyHeader", "MyValue"); HttpEntity<String> requestEntity = new HttpEntity<String>(requestBody, entityHeaders); ResponseEntity<List<Integer>> result = template.exchange("http://example.com", HttpMethod.POST, requestEntity, intList); assertEquals("Invalid POST result", expected, result.getBody()); assertEquals("Invalid Content-Type", MediaType.TEXT_PLAIN, result.getHeaders().getContentType()); assertEquals("Invalid Accept header", MediaType.TEXT_PLAIN_VALUE, requestHeaders.getFirst("Accept")); assertEquals("Invalid custom header", "MyValue", requestHeaders.getFirst("MyHeader")); assertEquals("Invalid status code", HttpStatus.OK, result.getStatusCode()); verify(response).close(); }