{"record":{"id":"a1bb5807aff87a29","repo":"spring-projects/spring-security","slug":"an-error-occurred-writing-the-oauth-2-0-authorizat","errorCode":null,"errorMessage":"An error occurred writing the OAuth 2.0 Authorization Server Metadata: + ex.getMessage()","messagePattern":"An error occurred writing the OAuth 2\\.0 Authorization Server Metadata: \\+ 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/OAuth2AuthorizationServerMetadataHttpMessageConverter.java","lineNumber":100,"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 Authorization Server Metadata: \" + ex.getMessage(), ex,\n\t\t\t\t\tinputMessage);\n\t\t}\n\t}\n\n\t@Override\n\tprotected void writeInternal(OAuth2AuthorizationServerMetadata authorizationServerMetadata,\n\t\t\tHttpOutputMessage outputMessage) throws HttpMessageNotWritableException {\n\t\ttry {\n\t\t\tMap<String, Object> authorizationServerMetadataResponseParameters = this.authorizationServerMetadataParametersConverter\n\t\t\t\t.convert(authorizationServerMetadata);\n\t\t\tthis.jsonMessageConverter.write(authorizationServerMetadataResponseParameters, 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 Authorization Server Metadata: \" + ex.getMessage(), ex);\n\t\t}\n\t}\n\n\t/**\n\t * Sets the {@link Converter} used for converting the OAuth 2.0 Authorization Server\n\t * Metadata parameters to an {@link OAuth2AuthorizationServerMetadata}.\n\t * @param authorizationServerMetadataConverter the {@link Converter} used for\n\t * converting to an {@link OAuth2AuthorizationServerMetadata}.\n\t */\n\tpublic final void setAuthorizationServerMetadataConverter(\n\t\t\tConverter<Map<String, Object>, OAuth2AuthorizationServerMetadata> authorizationServerMetadataConverter) {\n\t\tAssert.notNull(authorizationServerMetadataConverter, \"authorizationServerMetadataConverter cannot be null\");\n\t\tthis.authorizationServerMetadataConverter = authorizationServerMetadataConverter;\n\t}\n\n\t/**\n\t * Sets the {@link Converter} used for converting the","sourceCodeStart":82,"sourceCodeEnd":118,"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/OAuth2AuthorizationServerMetadataHttpMessageConverter.java#L82-L118","documentation":"OAuth2AuthorizationServerMetadataHttpMessageConverter.writeInternal wraps any exception raised while serializing OAuth2AuthorizationServerMetadata into a Map and writing it as JSON to the HTTP response. The converter catches Exception around the parameters conversion + jsonMessageConverter.write call and rethrows as org.springframework.http.converter.HttpMessageNotWritableException, preserving the cause (ex.getMessage() only appears in the message). This signals the authorization server metadata endpoint could not produce its JSON document.","triggerScenarios":"Calling OAuth2AuthorizationServerMetadataHttpMessageConverter.write() (via writeInternal) when the metadata-parameters Converter.convert() throws (invalid/null OAuth2AuthorizationServerMetadata fields) or when the underlying Jackson JSON converter fails to serialize the resulting Map<String,Object> to the HttpServletResponse output stream (I/O error, missing JSON converter, unserializable value).","commonSituations":"The configured ObjectMapper cannot serialize a custom metadata parameter added to the metadata object (e.g. a non-serializable object placed in the map); the servlet response was already committed or the client disconnected mid-write (broken pipe); someone replaced the default MappingJackson2HttpMessageConverter with a converter that cannot handle Map/JSON; a custom Converter supplied via setAuthorizationServerMetadataParametersConverter throws during convert().","solutions":["Inspect the cause (ex.getCause()) logged with this message — it names the real failure (serialization error vs I/O error) and fix that root problem.","Ensure a JSON-capable HttpMessageConverter (MappingJackson2HttpMessageConverter) with a working ObjectMapper is configured; check any custom converter set via setJsonMessageConverter.","Validate the OAuth2AuthorizationServerMetadata (issuer URL well-formed, required fields present, custom parameters serializable) before it reaches the metadata endpoint.","Review any custom Converter registered with setAuthorizationServerMetadataParametersConverter for thrown exceptions on your metadata instance.","If caused by client disconnect / committed response, treat as benign infrastructure noise rather than a code bug."],"exampleFix":"// before\n@Bean\nWebMvcConfigurer mvc() {\n  return new WebMvcConfigurer() {\n    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {\n      converters.add(new StringHttpMessageConverter()); // no JSON converter -> write fails\n    }\n  };\n}\n// after\n@Bean\nWebMvcConfigurer mvc() {\n  return new WebMvcConfigurer() {\n    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {\n      converters.add(new MappingJackson2HttpMessageConverter()); // JSON writer present\n    }\n  };\n}","handlingStrategy":"try-catch","validationCode":"// before serving metadata\nassert metadata != null && metadata.getIssuer() != null : \"issuer required\";\nobjectMapper.writeValueAsString(metadata.getParameters()); // throws early if unserializable","typeGuard":null,"tryCatchPattern":"try {\n  converter.write(metadata, MediaType.APPLICATION_JSON, outputMessage);\n} catch (HttpMessageNotWritableException e) {\n  logger.error(\"Failed to write authorization server metadata\", e.getCause());\n  response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);\n}","preventionTips":["Keep the default MappingJackson2HttpMessageConverter configured when using these converters.","Validate metadata (issuer URI, endpoints) at startup, not at request time.","Test serialization of all custom metadata parameters in unit tests with the production ObjectMapper.","Log the full cause, not just getMessage(), when this exception surfaces."],"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"}