Skip to content

HttpStatusCodeException - remove HttpStatusCode from message details #34209

@membersound

Description

@membersound

In terms of DRY (don't repeat yourself), I often feel that Spring adding the StatusCode in front of a HttpStatusCodeException is somehow counter-intuitive. And especially is against DRY, because the statuscode is anyway offered as http status code in the response.

Thus, I want to suggest the following test should succeed:

@Test
public void test() {
   assertEquals("junit", new HttpStatusCodeException(HttpStatusCode.FORBIDDEN, "junit"); //fails, because message = "403 junit"
}

Because as of now, if the HttpStatusCodeException is thrown from a @RestController, Spring renders a message like:

{ 
 ...
 "message": "403 junit"
 ...
}

Source:
HttpStatusCodeException.class:

	private static String getMessage(HttpStatusCode statusCode, String statusText) {
		if (!StringUtils.hasLength(statusText) && statusCode instanceof HttpStatus status) {
			statusText = status.getReasonPhrase();
		}
		return statusCode.value() + " " + statusText; //suggestion: remove the statusCode prefix here
	}
``

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions