2016-10-14 2 views
0

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. enter image description here 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" 
} 
+0

Das ist offensichtlich, oder? URL-Parameter startDate> endDate. – PeterMmm

+1

@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

Antwort

2

erstellen Basis Error Objekt (richtig kommentierte), das zurück zum UI.

class Error { 
    private String timestamp; 
    private Integer status; 
    private String error; 
    private String exception; 
    private String message; 
    private String path; 

    //constructors here, whatever 
..... 
} 

..then Ihre Exception mit etwas wie folgt ändern:

@ResponseStatus(HttpStatus.BAD_REQUEST) 
@ExceptionHandler(ValueNotAllowedException.class) 
@ResponseBody Error handleBadRequest(HttpServletRequest req, Exception ex) { 
    //Create and return your Error object here. 
    return new Error(/* populate with all the stuff you need here */); 
} 

hoffe, das hilft!

+0

ty, ich verstehe Ihre Vorgehensweise. Aber es ist nicht genau das, was ich brauche. Ich nehme an, es gibt native (im Frühjahr-Boot) Weg, um solche JSON zu generieren, weil ich nicht in Projekt-Smth wie diese Fehlerklasse sehen ... Was denken Sie? – Woland

+0

Sie erstellen die Fehlerklasse selbst. Du kannst es wirklich nennen. Also, erstellen Sie einfach eine Klasse, Ihr Modell für ein Fehlerrecht. Anschließend füllen Sie das Objekt mit Status, Nachricht, Ausnahmeinformationen usw. und senden es an die Benutzerschnittstelle. Beachten Sie die @ResponseBody-Annotation und wie der Rückgabetyp das Fehlerobjekt selbst ist. – Mechkov

+0

Die Error-Klasse wird intern in ein JSON-Objekt übersetzt. Wenn Sie im UI-Bereich JSON von Ihrem Backend anfordern, übersetzt Spring den Fehler in einen JSON. – Mechkov

Verwandte Themen