Search in sources :

Example 16 with HttpMessageNotWritableException

use of org.springframework.http.converter.HttpMessageNotWritableException in project spring-framework by spring-projects.

the class GsonHttpMessageConverter method writeInternal.

@Override
protected void writeInternal(Object o, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    Charset charset = getCharset(outputMessage.getHeaders());
    OutputStreamWriter writer = new OutputStreamWriter(outputMessage.getBody(), charset);
    try {
        if (this.jsonPrefix != null) {
            writer.append(this.jsonPrefix);
        }
        if (type != null) {
            this.gson.toJson(o, type, writer);
        } else {
            this.gson.toJson(o, writer);
        }
        writer.close();
    } catch (JsonIOException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}
Also used : HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) JsonIOException(com.google.gson.JsonIOException) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter)

Example 17 with HttpMessageNotWritableException

use of org.springframework.http.converter.HttpMessageNotWritableException in project pinpoint by naver.

the class MappingJsonpJackson2HttpMessageConverter method writeInternal.

@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    JsonGenerator jsonGenerator = getJsonGenerator(outputMessage);
    try {
        String callbackParam = getRequestParam(DEFAULT_CALLBACK_PARAMETER);
        if (callbackParam == null || "".equals(callbackParam)) {
            callbackParam = DEFAULT_CALLBACK_PARAMETER;
        }
        jsonGenerator.writeRaw(callbackParam);
        jsonGenerator.writeRaw(cbPrefix);
        getObjectMapper().writeValue(jsonGenerator, object);
        jsonGenerator.writeRaw(cbSuffix);
        jsonGenerator.writeRaw(cbEnd);
        jsonGenerator.flush();
    } catch (JsonProcessingException e) {
        throw new HttpMessageNotWritableException("Could not write JSON:" + e.getMessage(), e);
    }
}
Also used : HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 18 with HttpMessageNotWritableException

use of org.springframework.http.converter.HttpMessageNotWritableException in project java-chassis by ServiceComb.

the class TestCseHttpMessageConverter method testAll.

@Test
public void testAll() {
    MockUtil.getInstance().mockReflectionUtils();
    MockUtil.getInstance().mockCseClientHttpRequest();
    CseHttpMessageConverter lCseHttpMessageConverter = new CseHttpMessageConverter();
    lCseHttpMessageConverter.canWrite(null, null);
    lCseHttpMessageConverter.getSupportedMediaTypes();
    try {
        lCseHttpMessageConverter.read(this.getClass(), null);
    } catch (HttpMessageNotReadableException e) {
    // TODO Auto-generated catch block
    } catch (IOException e) {
    // TODO Auto-generated catch block
    }
    try {
        HttpOutputMessage httpOutputMessage = Mockito.mock(CseClientHttpRequest.class);
        lCseHttpMessageConverter.write(null, null, httpOutputMessage);
    } catch (HttpMessageNotWritableException | IOException e) {
    // TODO Auto-generated catch block
    }
    Assert.assertEquals(true, lCseHttpMessageConverter.canRead(null, null));
}
Also used : HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) HttpOutputMessage(org.springframework.http.HttpOutputMessage) IOException(java.io.IOException) Test(org.junit.Test)

Example 19 with HttpMessageNotWritableException

use of org.springframework.http.converter.HttpMessageNotWritableException in project openmrs-core by openmrs.

the class MappingJacksonHttpMessageConverter method writeInternal.

@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
    JsonGenerator jsonGenerator = this.objectMapper.getJsonFactory().createJsonGenerator(outputMessage.getBody(), encoding);
    // https://github.com/FasterXML/jackson-databind/issues/12
    if (this.objectMapper.getSerializationConfig().isEnabled(SerializationConfig.Feature.INDENT_OUTPUT)) {
        jsonGenerator.useDefaultPrettyPrinter();
    }
    try {
        if (this.jsonPrefix != null) {
            jsonGenerator.writeRaw(this.jsonPrefix);
        }
        this.objectMapper.writeValue(jsonGenerator, object);
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}
Also used : HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) JsonEncoding(org.codehaus.jackson.JsonEncoding) JsonGenerator(org.codehaus.jackson.JsonGenerator) JsonProcessingException(org.codehaus.jackson.JsonProcessingException)

Example 20 with HttpMessageNotWritableException

use of org.springframework.http.converter.HttpMessageNotWritableException in project spring-cloud-function by spring-cloud.

the class BetterGsonHttpMessageConverter method writeInternal.

@Override
protected void writeInternal(Object o, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    Charset charset = getCharset(outputMessage.getHeaders());
    OutputStreamWriter writer = new OutputStreamWriter(outputMessage.getBody(), charset);
    try {
        if (this.jsonPrefix != null) {
            writer.append(this.jsonPrefix);
        }
        if (type != null) {
            this.gson.toJson(o, type, writer);
        } else {
            this.gson.toJson(o, writer);
        }
        writer.flush();
    } catch (JsonIOException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}
Also used : HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) JsonIOException(com.google.gson.JsonIOException) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter)

Aggregations

HttpMessageNotWritableException (org.springframework.http.converter.HttpMessageNotWritableException)27 MediaType (org.springframework.http.MediaType)9 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)8 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)5 IOException (java.io.IOException)5 JsonEncoding (com.fasterxml.jackson.core.JsonEncoding)4 JsonEncoding (org.codehaus.jackson.JsonEncoding)4 JsonGenerator (org.codehaus.jackson.JsonGenerator)4 HttpOutputMessage (org.springframework.http.HttpOutputMessage)4 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)3 OutputStreamWriter (java.io.OutputStreamWriter)3 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)3 CsvSchema (com.fasterxml.jackson.dataformat.csv.CsvSchema)2 JsonIOException (com.google.gson.JsonIOException)2 Charset (java.nio.charset.Charset)2 ArrayList (java.util.ArrayList)2 Test (org.junit.jupiter.api.Test)2 HttpHeaders (org.springframework.http.HttpHeaders)2 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)2