Example usage for org.springframework.http.converter HttpMessageNotWritableException HttpMessageNotWritableException

List of usage examples for org.springframework.http.converter HttpMessageNotWritableException HttpMessageNotWritableException

Introduction

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

Prototype

public HttpMessageNotWritableException(String msg, @Nullable Throwable cause) 

Source Link

Document

Create a new HttpMessageNotWritableException.

Usage

From source file:org.ccntgrid.extend.spring.http.converter.json.MappingJackson2HttpMessageConverter.java

@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);
    try {// w w  w.  j  a v  a  2s.co m
        if (this.prefixJson) {
            jsonGenerator.writeRaw("{} && ");
        }
        this.objectMapper.writeValue(jsonGenerator, object);
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}

From source file:org.nobel.highriseapi.mapper.SimpleXmlHttpMessageConverter.java

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

    Writer out = new OutputStreamWriter(outputMessage.getBody(), getCharset(outputMessage.getHeaders()));

    try {//from  ww w  .  j  av a2s  .  c om
        this.serializer.write(o, out);
        out.close();
    } catch (Exception ex) {
        throw new HttpMessageNotWritableException("Could not write [" + o + "]", ex);
    }
}

From source file:com.cookbook.cq.dao.util.GsonHttpMessageConverter.java

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

    OutputStreamWriter writer = new OutputStreamWriter(outputMessage.getBody(),
            getCharset(outputMessage.getHeaders()));

    try {//from   ww w  .j  av a 2 s. c om
        if (this.prefixJson) {
            writer.append("{} && ");
        }
        Type typeOfSrc = getType();
        if (typeOfSrc != null) {
            this.gson.toJson(o, typeOfSrc, writer);
        } else {
            this.gson.toJson(o, writer);
        }
        writer.close();
    } catch (JsonIOException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}

From source file:org.kwet.giteway.mapper.PrettyMappingJacksonHttpMessageConverter.java

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

    JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
    JsonGenerator jsonGenerator = this.objectMapper.getJsonFactory()
            .createJsonGenerator(outputMessage.getBody(), encoding);
    try {/*  w w w  .ja v a  2s  . c  o  m*/
        if (this.prefixJson) {
            jsonGenerator.writeRaw("{} && ");
        }
        /**added by a.couette to enable prettyprint**/
        jsonGenerator.useDefaultPrettyPrinter();
        this.objectMapper.writeValue(jsonGenerator, object);
    } catch (IOException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}

From source file:tuwien.aic.crowdsourcing.util.MappingJackson2HttpMessageConverter.java

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

    JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
    JsonGenerator jsonGenerator = this.objectMapper.getFactory().createJsonGenerator(outputMessage.getBody(),
            encoding);//from w w  w  . j  a v  a  2  s.  co  m
    try {
        if (this.prefixJson) {
            jsonGenerator.writeRaw("{} && ");
        }
        if (this.prettyPrint) {
            jsonGenerator.useDefaultPrettyPrinter();
        }
        this.objectMapper.writeValue(jsonGenerator, object);
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}

From source file:org.jasig.portlet.courses.dao.xml.Jaxb2CourseSummaryHttpMessageConverter.java

@Override
protected void writeToResult(Object o, HttpHeaders headers, Result result) throws IOException {
    try {//from   ww  w .j a  v  a  2  s .c  om
        Class clazz = ClassUtils.getUserClass(o);
        Marshaller marshaller = createWrapperMarshaller(clazz);
        setCharset(headers.getContentType(), marshaller);
        marshaller.marshal(o, result);
    } catch (MarshalException ex) {
        throw new HttpMessageNotWritableException("Could not marshal [" + o + "]: " + ex.getMessage(), ex);
    } catch (JAXBException ex) {
        throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex);
    }
}

From source file:cn.loveapple.service.controller.member.action.MemberController.java

/**
 * //from  www  .j av a 2  s  .  c o m
 * 
 * @param session
 * @param model
 * @return
 */
@RequestMapping(value = "registComplete", method = RequestMethod.POST)
public String registComplete(HttpSession session, HttpServletRequest request, Model model, Locale locale) {

    LoveappleMemberModel member = (LoveappleMemberModel) session.getAttribute(LOVEAPPLE_MEMBER_TMP);

    if (member == null) {
        try {
            throw new NoSuchRequestHandlingMethodException(request);
        } catch (NoSuchRequestHandlingMethodException e) {
            throw new HttpMessageNotWritableException(e.getMessage(), e);
        }
    }
    member = memberCoreService.insertLoveappleMember(member);
    if (member == null) {
        throw new HttpMessageNotWritableException("can not regist member. "
                + ToStringBuilder.reflectionToString(session.getAttribute(LOVEAPPLE_MEMBER_TMP)));
    }
    session.removeAttribute(FORM);
    session.removeAttribute(LOVEAPPLE_MEMBER_TMP);

    // ??
    try {
        MimeMessage message = memberCoreService.sendRegistCertificationMail(member);
        log.info("Send mail: " + ToStringBuilder.reflectionToString(message));
    } catch (MailException e) {
        log.warn("?", e);
    }

    return "redirect:/member/certification";
}

From source file:org.resthub.web.converter.MappingJackson2XmlHttpMessageConverter.java

@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);

    // A workaround for JsonGenerators not applying serialization features
    // https://github.com/FasterXML/jackson-databind/issues/12
    if (this.objectMapper.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
        jsonGenerator.useDefaultPrettyPrinter();
    }//  www. ja v a 2 s .  c o m

    try {
        this.objectMapper.writeValue(jsonGenerator, object);
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write XML: " + ex.getMessage(), ex);
    }
}

From source file:com.wq.common.web.springmvc.MappingJackson2HttpMessageConverter.java

@SuppressWarnings("deprecation")
@Override/* w w w.j av a  2  s  .c om*/
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);

    // A workaround for JsonGenerators not applying serialization features
    // https://github.com/FasterXML/jackson-databind/issues/12
    if (this.objectMapper.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
        jsonGenerator.useDefaultPrettyPrinter();
    }

    try {
        if (this.prefixJson) {
            jsonGenerator.writeRaw("[] && ");
        }
        // jsonp??
        boolean isJSONP = !ObjectUtil.isEmpty(WebContext.getRequest().getParameter(DEFAULT_CALLBACK));
        if (isJSONP) {
            WebContext.getResponse().setContentType("application/x-javascript");
            jsonGenerator.writeRaw(WebContext.getRequest().getParameter(DEFAULT_CALLBACK) + "(");
        }
        this.objectMapper.writeValue(jsonGenerator, object);
        if (isJSONP) {
            outputMessage.getBody().write(')');
        }
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}

From source file:org.resthub.web.converter.MappingJackson2JsonHttpMessageConverter.java

@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);

    // A workaround for JsonGenerators not applying serialization features
    // https://github.com/FasterXML/jackson-databind/issues/12
    if (this.objectMapper.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
        jsonGenerator.useDefaultPrettyPrinter();
    }//from w w w  . j av  a  2  s  .  c  o  m

    try {
        if (this.prefixJson) {
            jsonGenerator.writeRaw("{} && ");
        }
        this.objectMapper.writeValue(jsonGenerator, object);
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}