{"record":{"id":"fac577a6e7e2407b","repo":"OpenFeign/feign","slug":"e-tostring-fac577","errorCode":null,"errorMessage":"${e.toString()}","messagePattern":"\\$\\{e\\.toString\\(\\)\\}","errorType":"exception","errorClass":"EncodeException","httpStatus":null,"severity":"error","filePath":"jaxb-jakarta/src/main/java/feign/jaxb/JAXBEncoder.java","lineNumber":67,"sourceCode":"  private final JAXBContextFactory jaxbContextFactory;\n\n  public JAXBEncoder(JAXBContextFactory jaxbContextFactory) {\n    this.jaxbContextFactory = jaxbContextFactory;\n  }\n\n  @Override\n  public void encode(Object object, Type bodyType, RequestTemplate template) {\n    if (!(bodyType instanceof Class)) {\n      throw new UnsupportedOperationException(\n          \"JAXB only supports encoding raw types. Found \" + bodyType);\n    }\n    try {\n      Marshaller marshaller = jaxbContextFactory.createMarshaller((Class<?>) bodyType);\n      StringWriter stringWriter = new StringWriter();\n      marshaller.marshal(object, stringWriter);\n      template.body(stringWriter.toString());\n    } catch (JAXBException e) {\n      throw new EncodeException(e.toString(), e);\n    }\n  }\n\n  @Override\n  public boolean canEncode(Object object, Type bodyType, RequestTemplate template) {\n    return Util.isXmlContentType(template);\n  }\n}\n","sourceCodeStart":49,"sourceCodeEnd":76,"githubUrl":"https://github.com/OpenFeign/feign/blob/e2a1e27560a1e68840c34f031afca88b36096e30/jaxb-jakarta/src/main/java/feign/jaxb/JAXBEncoder.java#L49-L76","documentation":"When JAXB marshalling of the request object fails (JAXBException), JAXBEncoder wraps it in a Feign EncodeException whose message is e.toString(). Typical causes are objects or nested fields lacking JAXB annotations, or a class not registered with the JAXBContextFactory.","triggerScenarios":"encode() is called (via customEncoder) with an object whose class has no @XmlRootElement/@XmlElement annotations, contains un-marshalable nested types, or does not match the declared body type.","commonSituations":"Forgetting @XmlRootElement on the body class; sending DTOs generated without JAXB annotations; mismatch between the class registered in the JAXBContextFactory and the actual runtime body class.","solutions":["Read e.getCause() for the precise JAXBException message and fix annotations accordingly.","Annotate the request class with @XmlRootElement/@XmlElement or register it via JAXBContextFactory.Builder.","Verify the object passed at runtime matches the interface's declared body type."],"exampleFix":"// before\nclass Item { private String name; } // marshal throws JAXBException\n// after\n@XmlRootElement\nclass Item { @XmlElement public String name; }","handlingStrategy":"try-catch","validationCode":"// before encoding\nif (!bodyClass.isAnnotationPresent(javax.xml.bind.annotation.XmlRootElement.class)\n    && !bodyClass.isAnnotationPresent(jakarta.xml.bind.annotation.XmlRootElement.class)) {\n  throw new IllegalArgumentException(bodyClass + \" lacks @XmlRootElement\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  encoder.encode(obj, type, template);\n} catch (EncodeException e) {\n  Throwable cause = e.getCause();\n  log.error(\"JAXB marshal failed: {}\", cause == null ? e.getMessage() : cause.getMessage());\n}","preventionTips":["Annotate every request DTO with @XmlRootElement and @XmlElement.","Register DTO classes via JAXBContextFactory.Builder.","Round-trip test each DTO: encode then decode in unit tests."],"tags":["jaxb","encode","marshal"],"backgroundTag":"xml-marshal-failed","analyzedSha":"e2a1e27560a1e68840c34f031afca88b36096e30","analyzedAt":"2026-09-10T12:37:37.238Z","contentChangedAt":"2026-09-10T12:37:37.238Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}