{"record":{"id":"804e631afe42c44f","repo":"spring-projects/spring-security","slug":"an-error-occurred-writing-the-oauth-2-0-device-aut","errorCode":null,"errorMessage":"An error occurred writing the OAuth 2.0 Device Authorization Response: ${ex.getMessage()}","messagePattern":"An error occurred writing the OAuth 2\\.0 Device Authorization Response: (.+?)","errorType":"http","errorClass":"HttpMessageNotWritableException","httpStatus":500,"severity":"error","filePath":"oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/http/converter/OAuth2DeviceAuthorizationResponseHttpMessageConverter.java","lineNumber":106,"sourceCode":"\t\tcatch (Exception ex) {\n\t\t\tthrow new HttpMessageNotReadableException(\n\t\t\t\t\t\"An error occurred reading the OAuth 2.0 Device Authorization Response: \" + ex.getMessage(), ex,\n\t\t\t\t\tinputMessage);\n\t\t}\n\t}\n\n\t@Override\n\tprotected void writeInternal(OAuth2DeviceAuthorizationResponse deviceAuthorizationResponse,\n\t\t\tHttpOutputMessage outputMessage) throws HttpMessageNotWritableException {\n\n\t\ttry {\n\t\t\tMap<String, Object> deviceAuthorizationResponseParameters = this.deviceAuthorizationResponseParametersConverter\n\t\t\t\t.convert(deviceAuthorizationResponse);\n\t\t\tthis.jsonMessageConverter.write(deviceAuthorizationResponseParameters, 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 Device Authorization Response: \" + ex.getMessage(), ex);\n\t\t}\n\t}\n\n\t/**\n\t * Sets the {@link Converter} used for converting the OAuth 2.0 Device Authorization\n\t * Response parameters to an {@link OAuth2DeviceAuthorizationResponse}.\n\t * @param deviceAuthorizationResponseConverter the {@link Converter} used for\n\t * converting to an {@link OAuth2DeviceAuthorizationResponse}\n\t */\n\tpublic final void setDeviceAuthorizationResponseConverter(\n\t\t\tConverter<Map<String, Object>, OAuth2DeviceAuthorizationResponse> deviceAuthorizationResponseConverter) {\n\t\tAssert.notNull(deviceAuthorizationResponseConverter, \"deviceAuthorizationResponseConverter cannot be null\");\n\t\tthis.deviceAuthorizationResponseConverter = deviceAuthorizationResponseConverter;\n\t}\n\n\t/**\n\t * Sets the {@link Converter} used for converting the","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/spring-projects/spring-security/blob/96852e8860138a482cb13d1479573f24ff6443c6/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/http/converter/OAuth2DeviceAuthorizationResponseHttpMessageConverter.java#L88-L124","documentation":"This HttpMessageNotWritableException is thrown by OAuth2DeviceAuthorizationResponseHttpMessageConverter.writeInternal when the OAuth2DeviceAuthorizationResponse object cannot be converted to its Map of parameters or serialized to JSON. It wraps any exception from the parameters converter or the JSON message converter during serialization.","triggerScenarios":"Calling writeInternal/write on an OAuth2DeviceAuthorizationResponseHttpMessageConverter when OAuth2DeviceAuthorizationResponseParametersConverter fails or jsonMessageConverter.write fails (e.g. no Jackson converter registered, IOException writing to output stream).","commonSituations":"Missing Jackson on the classpath so no JSON converter is configured, a custom parameters converter that throws, or the underlying HttpOutputMessage body stream is broken (client disconnected).","solutions":["Check the wrapped cause (ex.getCause()) for the concrete failure.","Ensure a Jackson-based MappingJackson2HttpMessageConverter is set via setJsonMessageConverter and Jackson is on the classpath.","If a custom parametersConverter was set, verify it handles null fields and produces Map<String,Object>.","Catch HttpMessageNotWritableException around the converter call and write a structured error response."],"exampleFix":"// before\nconverter.write(response, MediaType.APPLICATION_JSON, outputMessage);\n// after\ntry {\n    converter.write(response, MediaType.APPLICATION_JSON, outputMessage);\n} catch (HttpMessageNotWritableException ex) {\n    logger.error(\"Device authorization response write failed\", ex.getCause());\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    converter.write(response, MediaType.APPLICATION_JSON, outputMessage);\n} catch (HttpMessageNotWritableException ex) {\n    logger.error(\"Device auth response write failed\", ex.getCause());\n}","preventionTips":["Ensure Jackson is on the classpath and a MappingJackson2HttpMessageConverter is configured.","Test serialization of OAuth2DeviceAuthorizationResponse with null optional fields.","Handle client-disconnect IOExceptions upstream so they don't surface mid-write."],"tags":["oauth2","http-message-conversion","json","spring-security"],"backgroundTag":"json-serialization-failed","analyzedSha":"96852e8860138a482cb13d1479573f24ff6443c6","analyzedAt":"2026-09-10T23:25:23.477Z","contentChangedAt":"2026-09-10T23:25:23.477Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}