List of usage examples for org.springframework.http.converter HttpMessageConverter write
void write(T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException;
From source file:curly.artifactory.ArtifactoryTestHelper.java
public static String json(Object o, HttpMessageConverter<Object> httpMessageConverter) { MockHttpOutputMessage message = new MockHttpOutputMessage(); try {//from w w w . ja v a 2 s . c o m httpMessageConverter.write(o, jsonMediaType(), message); } catch (IOException ignore) { } return message.getBodyAsString(); }
From source file:de.iteratec.iteraplan.presentation.ajax.DojoUtils.java
public static void write(Object data, HttpServletResponse response) throws IOException { HttpMessageConverter<Object> converter = getConverter(); if (!converter.canWrite(data.getClass(), MediaType.APPLICATION_JSON)) { throw new IllegalArgumentException("The object cannot be serialized to JSON"); }//from www. ja va 2 s. c o m converter.write(data, MediaType.APPLICATION_JSON, new ServletServerHttpResponse(response)); }
From source file:org.zalando.riptide.Callback.java
@Override public void doWithRequest(final ClientHttpRequest request) throws IOException { final HttpHeaders headers = entity.getHeaders(); request.getHeaders().putAll(headers); @Nullable/*w ww . j a va 2 s. c o m*/ final T body = entity.getBody(); if (body == null) { return; } final Class<?> type = body.getClass(); @Nullable final MediaType contentType = headers.getContentType(); final Optional<HttpMessageConverter<T>> match = converters.stream() .filter(c -> c.canWrite(type, contentType)).map(this::cast).findFirst(); if (match.isPresent()) { final HttpMessageConverter<T> converter = match.get(); converter.write(body, contentType, request); } else { final String message = format( "Could not write request: no suitable HttpMessageConverter found for request type [%s]", type.getName()); if (contentType == null) { throw new RestClientException(message); } else { throw new RestClientException(format("%s and content type [%s]", message, contentType)); } } }
From source file:net.solarnetwork.web.test.SimpleCsvHttpMessageCoverterTest.java
@Test public void testEmptyModel() throws Exception { Map<String, Object> model = new LinkedHashMap<String, Object>(); HttpMessageConverter<Object> hmc = new SimpleCsvHttpMessageConverter(); hmc.write(model, CSV_MEDIA_TYPE, output); String result = response.getContentAsString(); assertEquals("", result); }
From source file:net.solarnetwork.web.test.SimpleCsvHttpMessageCoverterTest.java
@Test public void testSingleRowSingleColumn() throws Exception { Map<String, Object> model = new LinkedHashMap<String, Object>(); model.put("foo", "bar"); HttpMessageConverter<Object> hmc = new SimpleCsvHttpMessageConverter(); hmc.write(model, CSV_MEDIA_TYPE, output); String result = response.getContentAsString(); assertEquals("foo\nbar\n", result); }
From source file:net.solarnetwork.web.test.SimpleCsvHttpMessageCoverterTest.java
@Test public void testSingleRowBeanNoOrder() throws Exception { TestBean row = new TestBean("1", 2, "3"); HttpMessageConverter<Object> hmc = new SimpleCsvHttpMessageConverter(); hmc.write(row, CSV_MEDIA_TYPE, output); String result = response.getContentAsString(); assertFalse("Result should not be empty", result.length() == 0); assertFalse("Order should not be preserved", "one,two,three\n1,2,3\n".equals(result)); }
From source file:net.solarnetwork.web.test.SimpleCsvHttpMessageCoverterTest.java
@Test public void testSingleRowMap() throws Exception { Map<String, Object> row = new LinkedHashMap<String, Object>(); row.put("one", "1"); row.put("two", "2"); row.put("three", "3"); HttpMessageConverter<Object> hmc = new SimpleCsvHttpMessageConverter(); hmc.write(row, CSV_MEDIA_TYPE, output); String result = response.getContentAsString(); assertEquals("one,two,three\n1,2,3\n", result); }
From source file:net.solarnetwork.web.test.SimpleCsvHttpMessageCoverterTest.java
@Test public void testFieldWithDelimiter() throws Exception { Map<String, Object> row = new LinkedHashMap<String, Object>(); row.put("one", "1,1"); row.put("two", "2"); row.put("three", "3,3"); HttpMessageConverter<Object> hmc = new SimpleCsvHttpMessageConverter(); hmc.write(row, CSV_MEDIA_TYPE, output); String result = response.getContentAsString(); assertEquals("one,two,three\n\"1,1\",2,\"3,3\"\n", result); }
From source file:net.solarnetwork.web.test.SimpleCsvHttpMessageCoverterTest.java
@Test public void testMultiRowMap() throws Exception { List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>(); Map<String, Object> row = new LinkedHashMap<String, Object>(); row.put("one", "1"); row.put("two", "2"); row.put("three", "3"); rows.add(row);/*from ww w . j ava 2 s.c om*/ row = new LinkedHashMap<String, Object>(); row.put("one", "4"); row.put("two", "5"); row.put("three", "6"); rows.add(row); HttpMessageConverter<Object> hmc = new SimpleCsvHttpMessageConverter(); hmc.write(rows, CSV_MEDIA_TYPE, output); String result = response.getContentAsString(); assertEquals("one,two,three\n1,2,3\n4,5,6\n", result); }
From source file:org.cloudfoundry.identity.uaa.error.ExceptionReportHttpMessageConverter.java
@Override protected void writeInternal(ExceptionReport report, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { Exception e = report.getException(); Map<String, String> map = new HashMap<String, String>(); map.put("error", UaaStringUtils.getErrorName(e)); map.put("message", e.getMessage()); if (report.isTrace()) { StringWriter trace = new StringWriter(); e.printStackTrace(new PrintWriter(trace)); map.put("trace", trace.toString()); }/*from ww w .jav a 2s . co m*/ for (HttpMessageConverter<?> converter : messageConverters) { for (MediaType mediaType : converter.getSupportedMediaTypes()) { if (converter.canWrite(Map.class, mediaType)) { @SuppressWarnings({ "rawtypes", "unchecked" }) HttpMessageConverter<Map> messageConverter = (HttpMessageConverter<Map>) converter; messageConverter.write(map, mediaType, outputMessage); return; } } } }