OpenFeign/feign · error · EncodeException

${e.toString()}

Error message

${e.toString()}

What it means

Same as error 103 in the javax jaxb module: a JAXBException during Marshaller.marshal is converted to EncodeException with e.toString(). Indicates the request object cannot be marshalled to XML, typically due to missing or incorrect JAXB annotations.

Solutions

  1. Check getCause() for the exact JAXBException message.
  2. Add @XmlRootElement/@XmlElement annotations or register the class in JAXBContextFactory.Builder.
  3. Confirm the runtime object type matches the declared body type.

Example fix

// before
class Payload { private String data; } // marshal fails
// after
@XmlRootElement
public class Payload { @XmlElement public String data; }
Defensive patterns

Strategy: try-catch

Validate before calling

// before encoding
if (!obj.getClass().isAnnotationPresent(XmlRootElement.class)) {
  throw new IllegalArgumentException("request DTO must be @XmlRootElement annotated: " + obj.getClass());
}

Try / catch

try {
  api.post(payload);
} catch (EncodeException e) {
  log.error("JAXB marshal failed: {}", String.valueOf(e.getCause()));
}

Prevention

When it happens

Trigger: encode() with a body object whose class lacks @XmlRootElement, contains un-marshalable fields, or whose class is not registered with the JAXBContextFactory.

Common situations: DTOs without JAXB annotations; nested types not annotated; runtime class differing from the declared interface body type.

Related errors


AI-assisted analysis of OpenFeign/feign@e2a1e27560 (2026-09-10). Data as JSON: /api/errors/8b927aabcfa949e4. Report an issue: GitHub.

Appendix: source

Thrown at jaxb/src/main/java/feign/jaxb/JAXBEncoder.java:67

  private final JAXBContextFactory jaxbContextFactory;

  public JAXBEncoder(JAXBContextFactory jaxbContextFactory) {
    this.jaxbContextFactory = jaxbContextFactory;
  }

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

  @Override
  public boolean canEncode(Object object, Type bodyType, RequestTemplate template) {
    return Util.isXmlContentType(template);
  }
}

View on GitHub (pinned to e2a1e27560)