{"record":{"id":"47a9dd01434f050a","repo":"eclipse-vertx/vert.x","slug":"already-a-default-codec-registered-for-class","errorCode":null,"errorMessage":"Already a default codec registered for class ","messagePattern":"Already a default codec registered for class ","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"vertx-core/src/main/java/io/vertx/core/eventbus/impl/CodecManager.java","lineNumber":149,"sourceCode":"    checkSystemCodec(codec);\n    if (userCodecMap.containsKey(codec.name())) {\n      throw new IllegalStateException(\"Already a codec registered with name \" + codec.name());\n    }\n    userCodecMap.put(codec.name(), codec);\n  }\n\n  public void unregisterCodec(String name) {\n    Objects.requireNonNull(name);\n    userCodecMap.remove(name);\n  }\n\n  public <T> void registerDefaultCodec(Class<T> clazz, MessageCodec<T, ?> codec) {\n    Objects.requireNonNull(clazz);\n    Objects.requireNonNull(codec, \"codec\");\n    Objects.requireNonNull(codec.name(), \"code.name()\");\n    checkSystemCodec(codec);\n    if (defaultCodecMap.containsKey(clazz)) {\n      throw new IllegalStateException(\"Already a default codec registered for class \" + clazz);\n    }\n    if (userCodecMap.containsKey(codec.name())) {\n      throw new IllegalStateException(\"Already a codec registered with name \" + codec.name());\n    }\n    defaultCodecMap.put(clazz, codec);\n    userCodecMap.put(codec.name(), codec);\n  }\n\n  public void unregisterDefaultCodec(Class clazz) {\n    Objects.requireNonNull(clazz);\n    MessageCodec codec = defaultCodecMap.remove(clazz);\n    if (codec != null) {\n      userCodecMap.remove(codec.name());\n    }\n  }\n\n  public MessageCodec[] systemCodecs() {\n    return systemCodecs;","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/eclipse-vertx/vert.x/blob/fb308bd8c3f12c79f4ae89bef67fadf6c80d036e/vertx-core/src/main/java/io/vertx/core/eventbus/impl/CodecManager.java#L131-L167","documentation":"EventBus.registerDefaultCodec binds a Java class to a MessageCodec; only one default codec is allowed per class. This IllegalStateException is thrown when defaultCodecMap already contains an entry for the given class, so Vert.x refuses to silently overwrite the existing mapping.","triggerScenarios":"Calling eventBus().registerDefaultCodec(MyClass.class, codec) after MyClass.class was already mapped (by the same or another codec), e.g. during verticle redeploy or double initialization.","commonSituations":"Two verticles each registering a default codec for the same class; framework bootstrap code running more than once; tests that don't close the previous Vertx before starting a new registration round on a shared bus.","solutions":["Guard with a try-catch or check whether a codec is already registered for the class before registering.","Move default codec registration to a single, idempotent bootstrap step executed once per Vertx instance.","Register the default codec before deploying any verticles, and only in the verticle that owns the type.","Use unregisterDefaultCodec(clazz) before re-registering if replacement is intentional (and acceptable)."],"exampleFix":"// before\nverticles.forEach(v -> v.registerCodecs(bus)); // double registration\n// after\nAtomicBoolean registered = new AtomicBoolean();\nif (registered.compareAndSet(false, true)) {\n  bus.registerDefaultCodec(MyMsg.class, new MyMsgCodec());\n}","handlingStrategy":"validation","validationCode":"// wrap registration in an idempotent guard\nprivate static final Set<Class<?>> REGISTERED = ConcurrentHashMap.newKeySet();\nif (REGISTERED.add(MyMsg.class)) {\n  bus.registerDefaultCodec(MyMsg.class, new MyMsgCodec());\n}","typeGuard":null,"tryCatchPattern":"try {\n  bus.registerDefaultCodec(MyMsg.class, codec);\n} catch (IllegalStateException e) {\n  // already mapped; ignore\n}","preventionTips":["Register default codecs once, before deploying verticles","Keep one owner module responsible for each class-to-codec mapping","In tests, close the previous Vertx before re-registering"],"tags":["eventbus","codec","duplicate-registration","illegal-state"],"backgroundTag":"file-already-exists","analyzedSha":"fb308bd8c3f12c79f4ae89bef67fadf6c80d036e","analyzedAt":"2026-09-06T11:37:12.241Z","contentChangedAt":"2026-09-06T11:37:12.241Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}