spring-projects/spring-security · error · HttpMessageNotWritableException

An error occurred writing the UserInfo response: ${ex.getMes

Error message

An error occurred writing the UserInfo response: ${ex.getMessage()}

What it means

This HttpMessageNotWritableException is thrown by OidcUserInfoHttpMessageConverter.writeInternal when converting the OidcUserInfo to parameters or serializing them to JSON fails. It wraps the root cause's message. The library considers any failure to render the UserInfo response as a non-writable message.

Source

Thrown at oauth2/oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/oidc/http/converter/OidcUserInfoHttpMessageConverter.java:97

				.read(STRING_OBJECT_MAP.getType(), null, inputMessage);
			return this.userInfoConverter.convert(userInfoParameters);
		}
		catch (Exception ex) {
			throw new HttpMessageNotReadableException(
					"An error occurred reading the UserInfo response: " + ex.getMessage(), ex, inputMessage);
		}
	}

	@Override
	protected void writeInternal(OidcUserInfo oidcUserInfo, HttpOutputMessage outputMessage)
			throws HttpMessageNotWritableException {
		try {
			Map<String, Object> userInfoResponseParameters = this.userInfoParametersConverter.convert(oidcUserInfo);
			this.jsonMessageConverter.write(userInfoResponseParameters, STRING_OBJECT_MAP.getType(),
					MediaType.APPLICATION_JSON, outputMessage);
		}
		catch (Exception ex) {
			throw new HttpMessageNotWritableException(
					"An error occurred writing the UserInfo response: " + ex.getMessage(), ex);
		}
	}

	/**
	 * Sets the {@link Converter} used for converting the UserInfo parameters to an
	 * {@link OidcUserInfo}.
	 * @param userInfoConverter the {@link Converter} used for converting to an
	 * {@link OidcUserInfo}
	 */
	public final void setUserInfoConverter(Converter<Map<String, Object>, OidcUserInfo> userInfoConverter) {
		Assert.notNull(userInfoConverter, "userInfoConverter cannot be null");
		this.userInfoConverter = userInfoConverter;
	}

	/**
	 * Sets the {@link Converter} used for converting the {@link OidcUserInfo} to a
	 * {@code Map} representation of the UserInfo.

View on GitHub (pinned to 96852e8860)

Solutions

  1. Read the wrapped cause message to identify the actual failure
  2. Check that a JSON converter (Jackson) is available and configured
  3. Ensure all OidcUserInfo claim values are JSON-serializable primitives/collections
  4. Replace any customized converter via setJsonMessageConverter()/setUserInfoParametersConverter() with a working one

Example fix

// before: non-serializable claim
OidcUserInfo userInfo = new OidcUserInfo(Map.of("sub", "user", "obj", new Object()));
// after: JSON-friendly claims only
OidcUserInfo userInfo = new OidcUserInfo(Map.of("sub", "user"));
Defensive patterns

Strategy: try-catch

Validate before calling

// Validate claims are JSON-serializable before building OidcUserInfo
claims.values().forEach(v -> new ObjectMapper().writeValueAsString(v));

Type guard

boolean isSerializableClaimSet(Map<String,Object> claims) {
    try { new ObjectMapper().writeValueAsString(claims); return true; }
    catch (JsonProcessingException e) { return false; }
}

Try / catch

try {
    converter.write(oidcUserInfo, null, outputMessage);
} catch (HttpMessageNotWritableException e) {
    logger.error("UserInfo write failed: {}", e.getCause(), e);
}

Prevention

When it happens

Trigger: The userInfoParametersConverter throws while converting OidcUserInfo claims, or the jsonMessageConverter fails writing the resulting Map<String,Object> as JSON to the output message.

Common situations: Custom UserInfo claim values that Jackson cannot serialize; a missing or misconfigured JSON converter; a custom converter bean throwing during conversion.

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/57a8e770d7cc567c. Report an issue: GitHub.