Search in sources :

Example 1 with EncodeException

use of feign.codec.EncodeException in project feign by OpenFeign.

the class JacksonJaxbJsonEncoder method encode.

@Override
public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        jacksonJaxbJsonProvider.writeTo(object, bodyType.getClass(), null, null, APPLICATION_JSON_TYPE, null, outputStream);
        template.body(outputStream.toByteArray(), Charset.defaultCharset());
    } catch (IOException e) {
        throw new EncodeException(e.getMessage(), e);
    }
}
Also used : EncodeException(feign.codec.EncodeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 2 with EncodeException

use of feign.codec.EncodeException in project feign by OpenFeign.

the class JacksonEncoder method encode.

@Override
public void encode(Object object, Type bodyType, RequestTemplate template) {
    try {
        JavaType javaType = mapper.getTypeFactory().constructType(bodyType);
        template.body(mapper.writerFor(javaType).writeValueAsString(object));
    } catch (JsonProcessingException e) {
        throw new EncodeException(e.getMessage(), e);
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) EncodeException(feign.codec.EncodeException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with EncodeException

use of feign.codec.EncodeException in project feign by OpenFeign.

the class JAXBEncoder method encode.

@Override
public void encode(Object object, Type bodyType, RequestTemplate template) {
    if (!(bodyType instanceof Class)) {
        throw new UnsupportedOperationException("JAXB only supports encoding raw types. Found " + bodyType);
    }
    try {
        Marshaller marshaller = jaxbContextFactory.createMarshaller((Class) bodyType);
        StringWriter stringWriter = new StringWriter();
        marshaller.marshal(object, stringWriter);
        template.body(stringWriter.toString());
    } catch (JAXBException e) {
        throw new EncodeException(e.toString(), e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) JAXBException(javax.xml.bind.JAXBException) EncodeException(feign.codec.EncodeException)

Aggregations

EncodeException (feign.codec.EncodeException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 JAXBException (javax.xml.bind.JAXBException)1 Marshaller (javax.xml.bind.Marshaller)1