Lesen Content-Type application/json im Frühjahr

Wie in Sie versuchen-zu-nutzen-spring-boot-Ruhe-zu-Lesen-json-string-aus-post ich Lesen möchte eine json-Nutzlast, die aus einer POST-Anforderung in einer Feder RestController. Mit dem Inhaltstyp "text/plain" es ist kein problem, aber mit "application/json" die Deserialisierung schlägt fehl und ich bekomme eine MessageNotReadable Ausnahme. Aber tatsächlich ist der Inhalt könnte nicht einfacher sein, es ist nur ein leeres json-Objekt "{}". Könnte es sein, dass eine erforderliche Wandler fehlt?
Ich benutze Frühjahr Root-version 1.2.3.RELEASE.

Codierung Beispiel

@RequestMapping(value = "/deepdefinitions", method = POST, headers = "Accept=application/json")
@ResponseBody
public Definitions createOrUpdateDefinitions(HttpEntity<String> httpEntity) throws IOException { ... }

Curl-Aufruf

curl -H  "Content-type: application/json" -X POST -d '{}' http://localhost:8080/deepdefinitions

Fehler

{"timestamp":1434397457853,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Could not read JSON: Can not deserialize instance of java.lang.String out of START_OBJECT token\n at [Source: org.apache.catalina.connector.CoyoteInputStream@122f9ce3; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token\n at [Source: org.apache.catalina.connector.CoyoteInputStream@122f9ce3; line: 1, column: 1]","path":"/deepdefinitions"}
  • Sie verwenden sollten, Header = "Accept=application/json" in "@RequestMapping "und pass das serialisierte Objekt in "@RequestBody", um Ihre controller-Methode
  • Zeigt die genaue exception/StackTrace bitte.
  • die "Header" in RequestMapping keine änderung des Verhaltens. Update ich die Frage mit einem coding-Beispiel.
InformationsquelleAutor Gregor | 2015-06-15
Schreibe einen Kommentar