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

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

Introduction

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

Prototype

@Override
public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) 

Source Link

Document

This implementation checks if the given class is #supports(Class) supported , and if the #getSupportedMediaTypes() supported media types MediaType#includes(MediaType) include the given media type.

Usage

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

/**
 * Write json data to response stream/*w  w  w  .j  av 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));
    }

}