{"record":{"id":"2039b544ede0bf3d","repo":"OpenFeign/feign","slug":"s-is-not-a-type-supported-by-this-encoder","errorCode":null,"errorMessage":"%s is not a type supported by this encoder.","messagePattern":"(.+?) is not a type supported by this encoder\\.","errorType":"exception","errorClass":"EncodeException","httpStatus":null,"severity":"error","filePath":"json/src/main/java/feign/json/JsonEncoder.java","lineNumber":65,"sourceCode":" *                      .target(GitHub.class, \"https://api.github.com\");\n *\n *   JSONObject contributor = new JSONObject();\n *\n *   contributor.put(\"login\", \"radio-rogal\");\n *   contributor.put(\"contributions\", 0);\n *   github.create(\"openfeign\", \"feign\", contributor);\n * </pre>\n */\npublic class JsonEncoder implements Encoder, PredicatedEncoder {\n\n  @Override\n  public void encode(Object object, Type bodyType, RequestTemplate template)\n      throws EncodeException {\n    if (object == null) return;\n    if (object instanceof JSONArray || object instanceof JSONObject) {\n      template.body(object.toString());\n    } else {\n      throw new EncodeException(format(\"%s is not a type supported by this encoder.\", bodyType));\n    }\n  }\n\n  @Override\n  public boolean canEncode(Object object, Type bodyType, RequestTemplate template) {\n    return Util.isJsonContentType(template);\n  }\n}\n","sourceCodeStart":47,"sourceCodeEnd":74,"githubUrl":"https://github.com/OpenFeign/feign/blob/e2a1e27560a1e68840c34f031afca88b36096e30/json/src/main/java/feign/json/JsonEncoder.java#L47-L74","documentation":"JsonEncoder.encode() only serializes objects that are already org.json JSONObject or JSONArray instances; anything else throws an EncodeException saying the bodyType is not supported. Unlike Jackson/Gson encoders it performs no reflection-based data binding.","triggerScenarios":"Calling a POST/PUT Feign method whose body parameter is a POJO, Map, List, or String while using JsonEncoder as the encoder.","commonSituations":"Assuming JsonEncoder behaves like JacksonEncoder; sending request bodies after migrating encoders; passing a Map that was never wrapped in a JSONObject.","solutions":["Build the request body as an org.json JSONObject or JSONArray before encoding","Switch to JacksonEncoder or GsonEncoder for POJO/Map serialization","Write a custom Encoder that converts POJOs/Maps into JSONObject and delegates"],"exampleFix":"// before\napi.createUser(user); // user is a POJO -> EncodeException\n// after\napi.createUser(new JSONObject(pojoToMap(user)));\n// or: Feign.builder().encoder(new JacksonEncoder())","handlingStrategy":"type-guard","validationCode":"if (!(body instanceof JSONObject) && !(body instanceof JSONArray)) {\n  throw new IllegalArgumentException(\"JsonEncoder requires JSONObject/JSONArray, got \" + body.getClass());\n}","typeGuard":"boolean encodable(Object o){ return o instanceof JSONObject || o instanceof JSONArray; }","tryCatchPattern":"try { encoder.encode(body, bodyType, template); }\ncatch (EncodeException e) { throw new IllegalStateException(\"Wrap body in JSONObject or use JacksonEncoder\", e); }","preventionTips":["Always construct request bodies as org.json types with JsonEncoder","Prefer JacksonEncoder/GsonEncoder for POJOs and Maps","Add a unit test asserting the encoder supports each request body type"],"tags":["json","encoder","unsupported-type","feign"],"backgroundTag":"json-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"}