Example usage for org.springframework.http.converter StringHttpMessageConverter StringHttpMessageConverter

List of usage examples for org.springframework.http.converter StringHttpMessageConverter StringHttpMessageConverter

Introduction

In this page you can find the example usage for org.springframework.http.converter StringHttpMessageConverter StringHttpMessageConverter.

Prototype

public StringHttpMessageConverter(Charset defaultCharset) 

Source Link

Document

A constructor accepting a default charset to use if the requested content type does not specify one.

Usage

From source file:be.bittich.quote.config.SpringConfig.java

@Bean
public StringHttpMessageConverter stringHttpMessageConverter() {
    return new StringHttpMessageConverter(Charset.forName("UTF-8"));
}

From source file:fi.helsinki.opintoni.config.PageMetadataConfiguration.java

@Bean
public RestTemplate metaDataRestTemplate() {
    RestTemplate template = new RestTemplate();
    template.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
    return template;
}

From source file:cn.cuizuoli.gotour.config.WebMvcConfig.java

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
    converters.add(new MappingJackson2HttpMessageConverter());
    converters.add(new FormHttpMessageConverter());
}

From source file:org.intermine.app.service.RoboSpiceService.java

@Override
public RestTemplate createRestTemplate() {
    RestTemplate rtp = new RestTemplate();
    Charset utf8 = Charset.forName(CHARSET);

    ByteArrayHttpMessageConverter byteConv = new ByteArrayHttpMessageConverter();
    StringHttpMessageConverter stringConv = new StringHttpMessageConverter(utf8);

    FormHttpMessageConverter formConv = new FormHttpMessageConverter();
    formConv.setCharset(utf8);//from w w w .jav  a 2 s.c o m

    List<HttpMessageConverter<?>> converters = rtp.getMessageConverters();

    converters.add(byteConv);
    converters.add(stringConv);
    converters.add(formConv);

    rtp.setMessageConverters(converters);
    return rtp;
}

From source file:com.nebhale.cyclinglibrary.web.WebConfiguration.java

private HttpMessageConverter<?> createXmlConverter() {
    StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
    converter.setSupportedMediaTypes(Arrays.asList(ApplicationMediaType.POINTS_XML));

    return converter;
}

From source file:pl.eplan.config.WebMvcConfig.java

private void addStringConverter(List<HttpMessageConverter<?>> converters) {
    StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(UTF8);
    stringConverter.setWriteAcceptCharset(false);
    converters.add(stringConverter);//from w ww  . j  a  v a  2s. co m
}

From source file:cz.jirutka.spring.exhandler.support.HttpMessageConverterUtils.java

/**
 * Returns default {@link HttpMessageConverter} instances, i.e.:
 *
 * <ul>//from   w w  w  .j  a v  a2 s  .  c  o m
 *     <li>{@linkplain ByteArrayHttpMessageConverter}</li>
 *     <li>{@linkplain StringHttpMessageConverter}</li>
 *     <li>{@linkplain ResourceHttpMessageConverter}</li>
 *     <li>{@linkplain Jaxb2RootElementHttpMessageConverter} (when JAXB is present)</li>
 *     <li>{@linkplain MappingJackson2HttpMessageConverter} (when Jackson 2.x is present)</li>
 *     <li>{@linkplain org.springframework.http.converter.json.MappingJacksonHttpMessageConverter}
 *         (when Jackson 1.x is present and 2.x not)</li>
 * </ul>
 *
 * <p>Note: It does not return all of the default converters defined in Spring, but just thus
 * usable for exception responses.</p>
 */
@SuppressWarnings("deprecation")
public static List<HttpMessageConverter<?>> getDefaultHttpMessageConverters() {

    List<HttpMessageConverter<?>> converters = new ArrayList<>();

    StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
    stringConverter.setWriteAcceptCharset(false); // See SPR-7316

    converters.add(new ByteArrayHttpMessageConverter());
    converters.add(stringConverter);
    converters.add(new ResourceHttpMessageConverter());

    if (isJaxb2Present()) {
        converters.add(new Jaxb2RootElementHttpMessageConverter());
    }
    if (isJackson2Present()) {
        converters.add(new MappingJackson2HttpMessageConverter());

    } else if (isJacksonPresent()) {
        try {
            Class<?> clazz = Class
                    .forName("org.springframework.http.converter.json.MappingJacksonHttpMessageConverter");
            converters.add((HttpMessageConverter<?>) clazz.newInstance());

        } catch (ClassNotFoundException ex) {
            // Ignore it, this class is not available since Spring 4.1.0.
        } catch (InstantiationException | IllegalAccessException ex) {
            throw new IllegalStateException(ex);
        }
    }
    return converters;
}

From source file:org.springframework.hateoas.client.Traverson.java

private static final RestOperations createDefaultTemplate(List<MediaType> mediaTypes) {

    List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
    converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));

    if (mediaTypes.contains(MediaTypes.HAL_JSON)) {
        converters.add(getHalConverter());
    }/*from   www .jav a 2  s. c o  m*/

    RestTemplate template = new RestTemplate();
    template.setMessageConverters(converters);

    return template;
}

From source file:com.wbss.mycoffee.ui.services.ApiController.java

/**
 * Write json data to response stream// ww  w .  ja  v a2  s  .  c o  m
 * @param json
 * @param response
 * @throws Exception
 */
private void wirteJsonToResponse(String json, HttpServletResponse response) throws Exception {

    AbstractHttpMessageConverter<String> stringHttpMessageConverter = new StringHttpMessageConverter(
            Charset.forName("UTF-8"));
    MediaType jsonMimeType = new MediaType(MediaType.APPLICATION_JSON.getType(),
            MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("UTF-8"));

    if (stringHttpMessageConverter.canWrite(String.class, jsonMimeType)) {
        stringHttpMessageConverter.write(json, jsonMimeType, new ServletServerHttpResponse(response));
    }

}

From source file:com.kixeye.chassis.transport.MetricsTest.java

@Test
public void testMetricsPing() throws Exception {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("admin.enabled", "true");
    properties.put("admin.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("admin.hostname", "localhost");

    properties.put("websocket.enabled", "true");
    properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("websocket.hostname", "localhost");

    properties.put("http.enabled", "true");
    properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("http.hostname", "localhost");

    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    StandardEnvironment environment = new StandardEnvironment();
    environment.getPropertySources().addFirst(new MapPropertySource("default", properties));
    context.setEnvironment(environment);
    context.register(PropertySourcesPlaceholderConfigurer.class);
    context.register(TransportConfiguration.class);
    context.register(MetricsConfiguration.class);

    RestTemplate httpClient = new RestTemplate();

    try {/*  w w  w  . j a  v a  2  s. c  om*/
        context.refresh();

        httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
        for (MessageSerDe serDe : context.getBeansOfType(MessageSerDe.class).values()) {
            messageConverters.add(new SerDeHttpMessageConverter(serDe));
        }
        messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
        httpClient.setMessageConverters(messageConverters);

        ResponseEntity<String> response = httpClient.getForEntity(
                new URI("http://localhost:" + properties.get("admin.port") + "/metrics/ping"), String.class);

        logger.info("Got response: [{}]", response);

        Assert.assertEquals(response.getStatusCode().value(), HttpStatus.OK.value());
        Assert.assertEquals("pong".trim(), response.getBody().trim());
    } finally {
        context.close();
    }
}