Skip to content

Support headers with multiple values in ResponseStatusException #24261

@KanyCTa

Description

@KanyCTa

Affects: Spring-web 5.1.11 & 5.2.1

Spring-web got in version 5.1.11 very useful feature to transfer headers from ResponseExceptions to ResponseStatusExceptionHandler (first mentioned in #23741 and implements in 614c7b0).

Faced with problem: new mechanism can't transfer multivalues headers like

WWW-Authenticate: Negotiate, NTLM

in raw-style (in a Map<String, List<String>> in this case).
ResponseStatusException.getHeaders returns simple java.util.Map.
Nowadays I can transfer multivalues only by converting to delimeted string (for example) and from it in exception handler. Looks lile a bycicle :(

Listing in org.springframework.web.server.ResponseStatusException

/**
 * Return response headers associated with the exception, possibly required
 * for the given status code (e.g. "Allow", "Accept").
 * @since 5.1.11
 */
public Map<String, String> getHeaders() {
    return Collections.emptyMap();
}

Listing in org.springframework.web.server.handler.ResponseStatusExceptionHandler

private boolean updateResponse(ServerHttpResponse response, Throwable ex) {
    boolean result = false;
    HttpStatus status = determineStatus(ex);
    if (status != null) {
        if (response.setStatusCode(status)) {
            if (ex instanceof ResponseStatusException) {
                ((ResponseStatusException) ex).getHeaders()
                        .forEach((name, value) -> response.getHeaders().add(name, value));
            }
            result = true;
        }
    }
    else {
        Throwable cause = ex.getCause();
        if (cause != null) {
            result = updateResponse(response, cause);
        }
    }
    return result;
}

Note, that ServerHttpResponse.getHeaders() returns HttpHeaders that implements MultiValueMap<String, String>

Are any restrictions here for using MultiValueMap in ResponseStatusException.getHeaders() instead of Map?

Seems better to have more flexible mech

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: backportedAn issue that has been backported to maintenance branchestype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions