Java Code Examples for org.springframework.http.HttpOutputMessage

Following code examples demonstrate how to use org.springframework.http.HttpOutputMessagefrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.http.HttpOutputMessageand various code implementation of this class.

    @Override
    protected void writeInternal(AbstractRestModel<?> record,
            HttpOutputMessage outputMessage) throws IOException,
    HttpMessageNotWritableException {
        CSVPojoWriter out = new CSVPojoWriter(
                new CSVWriter(new OutputStreamWriter(outputMessage.getBody(),
                        Common.UTF8_CS), separator, quote));
        out.writeNext(record);
        out.close();
    } 


    @Override
    protected void writeInternal(JsonData t, HttpOutputMessage outputMessage)
            throws IOException, HttpMessageNotWritableException {
        StreamUtils.copy(t.toString(), Charset.forName("utf-8"), outputMessage.getBody());
    } 

    protected void writeInternal(String s, HttpOutputMessage outputMessage) throws IOException {
        outputMessage.getHeaders().setContentType(MediaType.TEXT_PLAIN);
        super.writeInternal(s, outputMessage);
    } 

    @Override
    protected void writeInternal(User user, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {
        String out = "hello:" + user.getId() + "-" + user.getName();
        outputMessage.getBody().write(out.getBytes());
    } 

    @Override
    protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException,
            HttpMessageNotWritableException {
        Charset charset = getEncoding(outputMessage.getHeaders().getContentType());
        JSON.writeJSONStringTo(obj, new OutputStreamWriter(outputMessage.getBody(), charset), getFeatures());
    } 

    @Override
    protected void writeInternal(ObjectStream<?> stream, HttpOutputMessage outputMessage)
            throws IOException, HttpMessageNotWritableException {
        CSVPojoWriter out = new CSVPojoWriter(new CSVWriter(new OutputStreamWriter(outputMessage.getBody(), Common.UTF8_CS),separator,quote));
        stream.streamData(out);
        out.close();
    } 

    @Override
    protected void writeInternal(Object o, HttpOutputMessage outputMessage)
            throws IOException, HttpMessageNotWritableException {
        super.writeInternal(o, outputMessage);
    } 

    @Override
    protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage)
            throws IOException, HttpMessageNotWritableException {

        if (ThreadJacksonMixInHolder.isContainsMixIn()) {
            objectMapper = ThreadJacksonMixInHolder.builderMapper();
        }
        JsonGenerator jsonGenerator = objectMapper.getFactory().createGenerator(outputMessage.getBody(),
                JsonEncoding.UTF8);

        if (objectMapper.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
            jsonGenerator.useDefaultPrettyPrinter();
        }
        try {
            objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            objectMapper.writeValue(jsonGenerator, object);
        } catch (JsonProcessingException ex) {
            throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
        }
    } 

Advertisement
Javadoc
Represents an HTTP output message, consisting of {@linkplain #getHeaders() headers and a writable {@

linkplain #getBody() body.

Typically implemented by an HTTP request handle on the client side, or an HTTP response handle on the server side. @author Arjen Poutsma @since 3.0

Read More
Advertisement