Ich habe folgendes Exception JSon:Convert HttpServletResponse
@ExceptionHandler(ValueNotAllowedException.class)
void handleBadRequests(HttpServletResponse response, ValueNotAllowedException ex) throws IOException {
response.sendError(HttpStatus.BAD_REQUEST.value(), ex.getMessage());
}
Antwort der Typ ist HTML
. Wie leicht wie die Reaktion in smth konvertieren:
(Antwort von einem anderen Controller in Projekt Meine Antwort ähnlich sein sollte.)
{
"timestamp": 1476462787425,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "Could not read document: Start date should be before or equal end date\n at [Source: [email protected]; line: 23, column: 16] (through reference chain: com.instinctools.mailtracker.endpoints.dto.QueryFilterDTO[\"startDate\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Start date should be before or equal end date\n at [Source: [email protected]; line: 23, column: 16] (through reference chain: com.instinctools.mailtracker.endpoints.dto.QueryFilterDTO[\"startDate\"])",
"path": "/statistics/clicks"
}
Das ist offensichtlich, oder? URL-Parameter startDate> endDate. – PeterMmm
@PeterMmm hast du nicht verstanden. Ich schreibe Backend auf Java. Im gesamten Projekt tritt bei einer Ausnahme die Antwort auf json auf (siehe Beispiel im Thema). Aber mein ExceptionHandler (es wurde für meinen Controller geschrieben) hat einen falschen Ausnahmestil (HTML-Seite). Wie kann ich 'response' Daten in json konvertieren? Ich verstehe nicht genau, wie andere Controller solche json-Informationen generieren, vielleicht ist es eine Standard-Spring-Boot-Option? – Woland