Search in sources :

Example 6 with HttpMessageNotWritableException

use of org.springframework.http.converter.HttpMessageNotWritableException in project commons by craftercms.

the class CrafterJackson2MessageConverter method writeInternal.

@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
    JsonGenerator jsonGenerator = this.getObjectMapper().getFactory().createGenerator(outputMessage.getBody(), encoding);
    // https://github.com/FasterXML/jackson-databind/issues/12
    if (this.getObjectMapper().isEnabled(SerializationFeature.INDENT_OUTPUT)) {
        jsonGenerator.useDefaultPrettyPrinter();
    }
    try {
        if (this.jsonPrefix != null) {
            jsonGenerator.writeRaw(this.jsonPrefix);
        }
        runAnnotations(object);
        ObjectWriter writer = this.getObjectMapper().writer(filter);
        writer.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(com.fasterxml.jackson.core.JsonEncoding) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 7 with HttpMessageNotWritableException

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

the class OAuth2ErrorHttpMessageConverter method writeInternal.

@Override
protected void writeInternal(OAuth2Error oauth2Error, HttpOutputMessage outputMessage) throws HttpMessageNotWritableException {
    try {
        Map<String, String> errorParameters = this.errorParametersConverter.convert(oauth2Error);
        this.jsonMessageConverter.write(errorParameters, STRING_OBJECT_MAP.getType(), MediaType.APPLICATION_JSON, outputMessage);
    } catch (Exception ex) {
        throw new HttpMessageNotWritableException("An error occurred writing the OAuth 2.0 Error: " + ex.getMessage(), ex);
    }
}
Also used : HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException)

Example 8 with HttpMessageNotWritableException

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

the class KotlinSerializationJsonHttpMessageConverter method encode.

private void encode(Object object, KSerializer<Object> serializer, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    try {
        String json = this.json.encodeToString(serializer, object);
        MediaType contentType = outputMessage.getHeaders().getContentType();
        outputMessage.getBody().write(json.getBytes(getCharsetToUse(contentType)));
        outputMessage.getBody().flush();
    } catch (IOException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}
Also used : HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) MediaType(org.springframework.http.MediaType) IOException(java.io.IOException) SerializationException(kotlinx.serialization.SerializationException) IOException(java.io.IOException) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException)

Example 9 with HttpMessageNotWritableException

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

the class AbstractWireFeedHttpMessageConverter method writeInternal.

@Override
protected void writeInternal(T wireFeed, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    Charset charset = (StringUtils.hasLength(wireFeed.getEncoding()) ? Charset.forName(wireFeed.getEncoding()) : DEFAULT_CHARSET);
    MediaType contentType = outputMessage.getHeaders().getContentType();
    if (contentType != null) {
        contentType = new MediaType(contentType, charset);
        outputMessage.getHeaders().setContentType(contentType);
    }
    WireFeedOutput feedOutput = new WireFeedOutput();
    try {
        Writer writer = new OutputStreamWriter(outputMessage.getBody(), charset);
        feedOutput.output(wireFeed, writer);
    } catch (FeedException ex) {
        throw new HttpMessageNotWritableException("Could not write WireFeed: " + ex.getMessage(), ex);
    }
}
Also used : HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) WireFeedOutput(com.rometools.rome.io.WireFeedOutput) FeedException(com.rometools.rome.io.FeedException) Charset(java.nio.charset.Charset) MediaType(org.springframework.http.MediaType) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 10 with HttpMessageNotWritableException

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

the class DefaultHandlerExceptionResolverTests method handleHttpMessageNotWritable.

@Test
public void handleHttpMessageNotWritable() {
    HttpMessageNotWritableException ex = new HttpMessageNotWritableException("foo");
    ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
    assertThat(mav).as("No ModelAndView returned").isNotNull();
    assertThat(mav.isEmpty()).as("No Empty ModelAndView returned").isTrue();
    assertThat(response.getStatus()).as("Invalid status code").isEqualTo(500);
}
Also used : HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) ModelAndView(org.springframework.web.servlet.ModelAndView) Test(org.junit.jupiter.api.Test)

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