2017-07-04 4 views
1

Ich habe ein Problem beim Versuch, eine POST-Anfrage über POSTMAN zu verarbeiten. In meinem Controller habe ich:Spring Controller bekommen BodyRequest Null

@ApiOperation(value = "xxxx", notes = "xxxx", response = 
    String.class, authorizations = { 
    @Authorization(value = "basicAuth") 
}, tags={ "saveCourse", }) 
@ApiResponses(value = { 
    @ApiResponse(code = 200, message = "successful operation", response = 
String.class), 
    @ApiResponse(code = 404, message = "Not found", response = 
String.class), 
    @ApiResponse(code = 405, message = "Invalid input", response = 
String.class), 
    @ApiResponse(code = 500, message = "Internal Server Error", response = 
String.class), 
    @ApiResponse(code = 200, message = "unexpected error", response = 
String.class) }) 
@RequestMapping(value = "/course/saveCourse", 
    produces = { "application/json"}, 
    consumes = { "application/json"}, 
    method = RequestMethod.POST) 
ResponseEntity<String> saveCourse(@ApiParam(value = "xxxxx" ,required=true) @RequestBody Course coure){ 
LOG.info(course.toString); 
} 

Klasse Course:

public class Course implements Serializable { 
/** 
* 
*/ 
private static final long serialVersionUID = 1L; 

@JsonProperty("prof") 
private Prof prof = null; 

@JsonProperty("students") 
private List<Strudent> students = new ArrayList<Strudent>(); 

// getters & setters 
// ... 
} 

Klasse Prof:

public class Prof implements Serializable { 
    @JsonProperty("profLastName") 
    private String profLastName = null; 

    @JsonProperty("profFirstName") 
    private String profFirstName = null; 

    @JsonProperty("age") 
    private int age = null; 

    // getters & setters 
} 

Klasse Student:

public class Student implements Serializable { 
    @JsonProperty("studentId") 
    private String studentId = null; 

    @JsonProperty("studentName") 
    private String studentName = null; 

    @JsonProperty("studAge") 
    private int studAge = null; 

    // getters & setters 
    // ... 
} 

in POSTMAN sende ich ein POST re mit dem Header Suche:

Content-Type : application/json 

der Körper:

{ 
    "prof": { 
     "profLastName":"test", 
     "profFirstName":"test", 
     "age":"30" 
    }, 
    "students" :[ 
    "{'studentId':'0','studentName':'','studAge':'00'}", 
    "{'studentId':'2','studentName':'','studAge':'21'}", 
    "{'studentId':'4','studentName':'','studAge':'40'}", 
    "{'studentId':'6','studentName':'','studAge':'60'}" 
    ] 
} 

Wenn ich verarbeiten die Anforderung ich die RequestBody null bin immer:

[http-nio-xxxx-exec-4 INFO com.test.myControllerIml] - Klasse Course { prof: null Studenten: []}

+0

verwenden sollte, wenn ich meine Anfrage debuggen: CharStreams.toString (request.getReader()) ich die json bin immer, dass ich geschickt, so wohl die Frage ist in der Zuordnung. N.B: Wir verwenden Swagger, um unsere API zu generieren. – anonymouslyGeek

Antwort

0

Sie fordern Körper ist falsch Sie

{ 
    "prof": { 
     "profLastName":"test", 
     "profFirstName":"test", 
     "age":"30" 
    }, 
    "students" :[ 
    {"studentId":"0","studentName":"","studAge":"00"}, 
    {"studentId":"2","studentName":"","studAge":"21"}, 
    {"studentId":"4","studentName":"","studAge":"40"}, 
    {"studentId":"6","studentName":"","studAge":"60"} 
    ] 
} 
+0

Hallo Melad, Ich habe Ihre Lösung versucht, aber hatte das gleiche Ergebnis. Vielen Dank – anonymouslyGeek

+0

Ihr Code-Code funktioniert gut für mich. Ich habe einige Kompilierungsfehler bemerkt und lokal behoben. –