{"record":{"id":"c1b91443554d78d6","repo":"spring-projects/spring-security","slug":"an-error-occurred-writing-the-oauth-2-0-client-reg","errorCode":null,"errorMessage":"An error occurred writing the OAuth 2.0 Client Registration: + ex.getMessage()","messagePattern":"An error occurred writing the OAuth 2\\.0 Client Registration: \\+ ex\\.getMessage\\(\\)","errorType":"http","errorClass":"HttpMessageNotWritableException","httpStatus":500,"severity":"error","filePath":"oauth2/oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/http/converter/OAuth2ClientRegistrationHttpMessageConverter.java","lineNumber":109,"sourceCode":"\t\t}\n\t\tcatch (Exception ex) {\n\t\t\tthrow new HttpMessageNotReadableException(\n\t\t\t\t\t\"An error occurred reading the OAuth 2.0 Client Registration: \" + ex.getMessage(), ex,\n\t\t\t\t\tinputMessage);\n\t\t}\n\t}\n\n\t@Override\n\tprotected void writeInternal(OAuth2ClientRegistration clientRegistration, HttpOutputMessage outputMessage)\n\t\t\tthrows HttpMessageNotWritableException {\n\t\ttry {\n\t\t\tMap<String, Object> clientRegistrationParameters = this.clientRegistrationParametersConverter\n\t\t\t\t.convert(clientRegistration);\n\t\t\tthis.jsonMessageConverter.write(clientRegistrationParameters, STRING_OBJECT_MAP.getType(),\n\t\t\t\t\tMediaType.APPLICATION_JSON, outputMessage);\n\t\t}\n\t\tcatch (Exception ex) {\n\t\t\tthrow new HttpMessageNotWritableException(\n\t\t\t\t\t\"An error occurred writing the OAuth 2.0 Client Registration: \" + ex.getMessage(), ex);\n\t\t}\n\t}\n\n\t/**\n\t * Sets the {@link Converter} used for converting the OAuth 2.0 Client Registration\n\t * parameters to an {@link OAuth2ClientRegistration}.\n\t * @param clientRegistrationConverter the {@link Converter} used for converting to an\n\t * {@link OAuth2ClientRegistration}\n\t */\n\tpublic final void setClientRegistrationConverter(\n\t\t\tConverter<Map<String, Object>, OAuth2ClientRegistration> clientRegistrationConverter) {\n\t\tAssert.notNull(clientRegistrationConverter, \"clientRegistrationConverter cannot be null\");\n\t\tthis.clientRegistrationConverter = clientRegistrationConverter;\n\t}\n\n\t/**\n\t * Sets the {@link Converter} used for converting the {@link OAuth2ClientRegistration}","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/spring-projects/spring-security/blob/96852e8860138a482cb13d1479573f24ff6443c6/oauth2/oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/http/converter/OAuth2ClientRegistrationHttpMessageConverter.java#L91-L127","documentation":"OAuth2ClientRegistrationHttpMessageConverter.writeInternal wraps any exception raised while converting an OAuth2ClientRegistration to its parameter Map and writing it as JSON to the HTTP output stream. Failures in the parameters Converter.convert() or the Jackson-based jsonMessageConverter.write() are rethrown as org.springframework.http.converter.HttpMessageNotWritableException with the cause preserved. It indicates the client registration payload could not be serialized to the response/request.","triggerScenarios":"Calling OAuth2ClientRegistrationHttpMessageConverter.write() (via writeInternal) when clientRegistrationParametersConverter.convert() throws (null/invalid registration fields) or jsonMessageConverter.write() fails to serialize the Map<String,Object> as JSON (no JSON converter, I/O error on the stream, unserializable custom parameter).","commonSituations":"A custom Converter registered via setClientRegistrationParametersConverter emits values the ObjectMapper cannot serialize; the connection is broken mid-write (broken pipe); the mapping Jackson converter was removed or replaced in message-converters config; writing the client registration as part of an OIDC client-registration request to a provider whose connection dropped.","solutions":["Read ex.getCause() to find the real failure (serialization vs I/O) and address it directly.","Configure a working JSON HttpMessageConverter (e.g. MappingJackson2HttpMessageConverter) or pass one via setJsonMessageConverter.","Ensure all values in the registration (including custom parameters) are JSON-serializable and required fields (client_id, endpoints) are set before writing.","Review any custom clientRegistrationParametersConverter for exceptions on your registration instance.","Retry on transient I/O failures such as broken connections when writing to a remote registration endpoint."],"exampleFix":"// before\nclientRegistration.getAdditionalParameters().put(\"customInfo\", new File(\"/etc/config\")); // unserializable\n// after\nclientRegistration.getAdditionalParameters().put(\"customInfo\", \"/etc/config\"); // JSON-serializable value","handlingStrategy":"try-catch","validationCode":"// before writing\nassert registration.getClientId() != null : \"client_id required\";\nregistration.getAdditionalParameters().values().forEach(v -> objectMapper.valueToTree(v)); // fails fast on unserializable values","typeGuard":null,"tryCatchPattern":"try {\n  converter.write(registration, MediaType.APPLICATION_JSON, outputMessage);\n} catch (HttpMessageNotWritableException e) {\n  logger.error(\"Failed to write client registration\", e.getCause());\n  throw new OAuth2ClientRegistrationException(\"Serialization failed\", e);\n}","preventionTips":["Only place JSON-serializable values (primitives, strings, collections, Jackson beans) in additional parameters.","Verify required registration fields before building the request.","Keep the Jackson JSON converter on the classpath and wired in.","Test round-trip write/read of registration documents in CI."],"tags":["spring-security","oauth2","http-message-converter","json-serialization"],"backgroundTag":"json-marshal-failed","analyzedSha":"96852e8860138a482cb13d1479573f24ff6443c6","analyzedAt":"2026-09-10T23:25:23.477Z","contentChangedAt":"2026-09-10T23:25:23.477Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}