Some time ago I wrote an answer that could be of help: it is not related to the SonarQube error, but on how to adapt the error messages returned by RestTemplate
.
The idea is configuring RestTemplate
to use a custom ResponseErrorHandler
. Adapted from the answer:
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.UnknownHttpStatusCodeException;
public class CustomRestTemplateResponseErrorHandler extends DefaultResponseErrorHandler {
// This overloaded method version is only available since Spring 5.0
// For previous versions of the library you can override
// handleError(ClientHttpResponse response) instead
@Override
protected void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException {
String statusText = response.getStatusText();
HttpHeaders headers = response.getHeaders();
byte[] body = getResponseBody(response);
Charset charset = getCharset(response);
String message = getErrorMessage(statusCode.value(), statusText, body, charset);
switch (statusCode.series()) {
case CLIENT_ERROR:
throw HttpClientErrorException.create(message, statusCode, statusText, headers, body, charset);
case SERVER_ERROR:
throw HttpServerErrorException.create(message, statusCode, statusText, headers, body, charset);
default:
throw new UnknownHttpStatusCodeException(message, statusCode.value(), statusText, headers, body, charset);
}
}
/**
* Return error message with details from the response body:
* <pre>
* 404 Not Found: [{'id': 123, 'message': 'actual mesage']
* </pre>
*
* In contrast to <code>DefaultResponseErrorHandler</code>, the message will not be truncated.
*/
private String getErrorMessage(
int rawStatusCode, String statusText, @Nullable byte[] responseBody, @Nullable Charset charset) {
// Build and return your custom json error message here
}
}
Then, configure RestTemplate
to use this custom ResponseErrorHandler
:
RestTemplate restTemplate = new RestTemplate();
ResponseErrorHandler errorHandler = new CustomRestTemplateResponseErrorHandler();
restTemplate.setErrorHandler(errorHandler);
// use your RestTemplate normally
Please, as also stated in the mentioned answer, consider read this related SO question and this or this related blog posts.
Using this approach the returned error should be properly formatted without the need of using PrintWriter
directly, Spring will take care of everything to return this message to the client in a proper way without any SonarQube issue.
Sometimes resolving a SonarQube rule validation error could be cornerstone: in this specific use case, if you are sure that the information returned is safe, you can probably ignoring it. In any case, to avoid any potential problem, be sure that your client sanitizes the returned information when you use it using the means provided by the actual framework you are using.