spring-projects/spring-security · error · HttpMessageNotWritableException

An error occurred writing the OAuth 2.0 Client Registration:

Error message

An error occurred writing the OAuth 2.0 Client Registration: + ex.getMessage()

What it means

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.

Source

Thrown at oauth2/oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/http/converter/OAuth2ClientRegistrationHttpMessageConverter.java:109

		}
		catch (Exception ex) {
			throw new HttpMessageNotReadableException(
					"An error occurred reading the OAuth 2.0 Client Registration: " + ex.getMessage(), ex,
					inputMessage);
		}
	}

	@Override
	protected void writeInternal(OAuth2ClientRegistration clientRegistration, HttpOutputMessage outputMessage)
			throws HttpMessageNotWritableException {
		try {
			Map<String, Object> clientRegistrationParameters = this.clientRegistrationParametersConverter
				.convert(clientRegistration);
			this.jsonMessageConverter.write(clientRegistrationParameters, STRING_OBJECT_MAP.getType(),
					MediaType.APPLICATION_JSON, outputMessage);
		}
		catch (Exception ex) {
			throw new HttpMessageNotWritableException(
					"An error occurred writing the OAuth 2.0 Client Registration: " + ex.getMessage(), ex);
		}
	}

	/**
	 * Sets the {@link Converter} used for converting the OAuth 2.0 Client Registration
	 * parameters to an {@link OAuth2ClientRegistration}.
	 * @param clientRegistrationConverter the {@link Converter} used for converting to an
	 * {@link OAuth2ClientRegistration}
	 */
	public final void setClientRegistrationConverter(
			Converter<Map<String, Object>, OAuth2ClientRegistration> clientRegistrationConverter) {
		Assert.notNull(clientRegistrationConverter, "clientRegistrationConverter cannot be null");
		this.clientRegistrationConverter = clientRegistrationConverter;
	}

	/**
	 * Sets the {@link Converter} used for converting the {@link OAuth2ClientRegistration}

View on GitHub (pinned to 96852e8860)

Solutions

  1. Read ex.getCause() to find the real failure (serialization vs I/O) and address it directly.
  2. Configure a working JSON HttpMessageConverter (e.g. MappingJackson2HttpMessageConverter) or pass one via setJsonMessageConverter.
  3. Ensure all values in the registration (including custom parameters) are JSON-serializable and required fields (client_id, endpoints) are set before writing.
  4. Review any custom clientRegistrationParametersConverter for exceptions on your registration instance.
  5. Retry on transient I/O failures such as broken connections when writing to a remote registration endpoint.

Example fix

// before
clientRegistration.getAdditionalParameters().put("customInfo", new File("/etc/config")); // unserializable
// after
clientRegistration.getAdditionalParameters().put("customInfo", "/etc/config"); // JSON-serializable value
Defensive patterns

Strategy: try-catch

Validate before calling

// before writing
assert registration.getClientId() != null : "client_id required";
registration.getAdditionalParameters().values().forEach(v -> objectMapper.valueToTree(v)); // fails fast on unserializable values

Try / catch

try {
  converter.write(registration, MediaType.APPLICATION_JSON, outputMessage);
} catch (HttpMessageNotWritableException e) {
  logger.error("Failed to write client registration", e.getCause());
  throw new OAuth2ClientRegistrationException("Serialization failed", e);
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


AI-assisted analysis of spring-projects/spring-security@96852e8860 (2026-09-10). Data as JSON: /api/errors/c1b91443554d78d6. Report an issue: GitHub.