{"record":{"id":"1b91a98d3853206b","repo":"OpenFeign/feign","slug":"jaxb-only-supports-encoding-raw-types-found-bod","errorCode":null,"errorMessage":"JAXB only supports encoding raw types. Found ${bodyType}","messagePattern":"JAXB only supports encoding raw types\\. Found (.+?)","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"jaxb-jakarta/src/main/java/feign/jaxb/JAXBEncoder.java","lineNumber":58,"sourceCode":" *     .encoder(new JAXBEncoder(jaxbFactory))\n *     .target(MyApi.class, \"http://api\");\n * </pre>\n *\n * <p>The JAXBContextFactory should be reused across requests as it caches the created JAXB\n * contexts.\n */\npublic class JAXBEncoder implements Encoder, PredicatedEncoder {\n\n  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":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/OpenFeign/feign/blob/e2a1e27560a1e68840c34f031afca88b36096e30/jaxb-jakarta/src/main/java/feign/jaxb/JAXBEncoder.java#L40-L76","documentation":"JAXBEncoder.encode only supports request body types that are plain Classes (JAXB-mappable raw types). If the declared body type is a ParameterizedType such as List<Foo> or Map<String,Foo>, it throws UnsupportedOperationException because JAXB cannot marshal generic types without a root wrapper element.","triggerScenarios":"Declaring a Feign method whose request body is a generic collection or other ParameterizedType while using JAXBEncoder, then invoking encode (via Feign's customEncoder path).","commonSituations":"Trying to POST a List<MyDto> with JAXB encoding; developers expect automatic collection handling like JSON encoders provide.","solutions":["Encode a single @XmlRootElement-annotated root object instead of a generic collection.","Introduce a wrapper DTO containing the collection as an @XmlElement field and post that.","Use a JSON encoder module for generic types and reserve JAXB for raw annotated types."],"exampleFix":"// before\n@POST void send(List<Item> items);\n// after\n@XmlRootElement\nclass ItemList { @XmlElement(name=\"item\") List<Item> items; }\n@POST void send(ItemList items);","handlingStrategy":"type-guard","validationCode":"// before encoding\nif (!(bodyType instanceof Class)) {\n  throw new IllegalArgumentException(\"JAXB encoder requires a raw Class body type, got: \" + bodyType);\n}","typeGuard":"boolean isRawType(java.lang.reflect.Type t) {\n  return t instanceof Class;\n}","tryCatchPattern":"try {\n  encoder.encode(obj, type, template);\n} catch (UnsupportedOperationException e) {\n  // switch to a wrapper @XmlRootElement DTO or a JSON encoder\n}","preventionTips":["Only declare raw, JAXB-annotated classes as request bodies when using JAXB.","Wrap collections in an @XmlRootElement DTO.","Document that the JAXB module does not handle generic types."],"tags":["jaxb","encode","unsupported-type"],"backgroundTag":"unsupported-operation","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"}