Encodes requests using JAXB. Basic example with with Feign.Builder: JAXBContextFactory jaxbFactory = new JAXBContextFactory.Builder() .withMarshallerJAXBEncoding("UTF-8") .withMarshallerSchemaLocation("http://apihost http://apihost/schema.xsd") .build(); api = Feign.buil
| 43 | * contexts. |
| 44 | */ |
| 45 | public class JAXBEncoder implements Encoder { |
| 46 | |
| 47 | private final JAXBContextFactory jaxbContextFactory; |
| 48 | |
| 49 | public JAXBEncoder(JAXBContextFactory jaxbContextFactory) { |
| 50 | this.jaxbContextFactory = jaxbContextFactory; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public void encode(Object object, Type bodyType, RequestTemplate template) { |
| 55 | if (!(bodyType instanceof Class)) { |
| 56 | throw new UnsupportedOperationException( |
| 57 | "JAXB only supports encoding raw types. Found " + bodyType); |
| 58 | } |
| 59 | try { |
| 60 | Marshaller marshaller = jaxbContextFactory.createMarshaller((Class<?>) bodyType); |
| 61 | StringWriter stringWriter = new StringWriter(); |
| 62 | marshaller.marshal(object, stringWriter); |
| 63 | template.body(stringWriter.toString()); |
| 64 | } catch (JAXBException e) { |
| 65 | throw new EncodeException(e.toString(), e); |
| 66 | } |
| 67 | } |
| 68 | } |
nothing calls this directly
no outgoing calls
no test coverage detected