Ich bekomme 400 Http Antwort, wenn ich das ungültige Json-Format,
übergeben möchte Ich möchte die benutzerdefinierte JSON-Nachricht anstelle dieser, kann jemand beraten, wie im Frühjahr zu tun 4.1?Wie 400 Fehler im Frühjahr behandelt MVC
Handling Exception mit ControllerAdvice, aber es funktioniert nicht.
@ControllerAdvice
public class GlobalControllerExceptionHandler {
@ExceptionHandler({org.springframework.http.converter.HttpMessageNotReadableException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public String resolveException() {
return "error";
}
}
feder config.xml unterhalb
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
</map>
</property>
<property name="defaultViews">
<list>
<!-- Renders JSON View -->
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</list>
</property>
</bean>
unten Json Anforderungs- und Antwort von WebSphere Application-Server (7.0) gegeben gegeben.
Request 1: Empty json request : {}
Response Status Code: 400 Bad Request
Response Message : Json request contains invalid data:null
Request 2:Invalid format of Json Request : {"data":,"name":"java"}
Response Status Code: 400 Bad Request
Response or Exception message :
nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unexpected character (',' (code 44)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: [email protected]; line: 5, column: 57]
ähnliche Frage wie unten Link Using Spring MVC, accepting POST requests with bad JSON leads to a default 400 error code server page being returned
Was ist das Problem bei der Verwendung von '@ ControllerAdvice'? –
@ControllerAdvice nicht ausgelöst. –
1. Werden die Pakete nach Anmerkungen durchsucht? 2. Wird diese Ausnahme ausgelöst? – sergiu