Example usage for org.springframework.http.converter AbstractHttpMessageConverter write

List of usage examples for org.springframework.http.converter AbstractHttpMessageConverter write

Introduction

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

Prototype

@Override
public final void write(final T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException 

Source Link

Document

This implementation sets the default headers by calling #addDefaultHeaders , and then calls #writeInternal .

Usage

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

/**
 * Write json data to response stream//from w  w  w .  j ava  2  s  .co 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));
    }

}