The connection between client and server works fine and the correct function addEpic is called on the server. The problem is, that just a new instance of Epic is created on the server, but the attributes from the client are not used. @RequestBody seems to be the problem. Shouldn't convert @RequestBody automatically from the json data to the specified class? Is the fundamental problem that Epic is a @Entity class?
It could also be that body is wrongly generated at the client. console.log(body) shows:
{"epic":{"id":"f97d885a-410f-fa6d-7adc-291e63d35341", "name":"OurName"}}
But my swagger-ui shows for body model shema:
{"id":"string", "name":"string"}
Client
addEpic(epic:Epic):Observable<any> {
let body = JSON.stringify({epic});
let headers = new Headers({'Content-Type': 'application/json'});
let options = new RequestOptions({headers: headers});
return this.http.post(url + "addEpic", body, options)
.map(this.extractHeader)
.catch(this.handleError);
}
export class Epic {
id: string;
name: string;
}
Server
@RequestMapping(value="/addEpic", method = RequestMethod.POST)
public ResponseEntity<Void> addEpic(@RequestBody Epic epic) {
// Here it seems that constructor of epic is called and a new instance is created
epicRepository.saveAndFlush(epic);
return new ResponseEntity<Void>(HttpStatus.CREATED);
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Epic implements Serializable {
private static final long serialVersionUID = -670305953055479441L;
@Column
private String id;
@Column
private String name
}
Your entity Epic has two properties 'id' and 'name'. In JSON :
{"id":"string", "name":"string"}
This is exactly what Swagger showed you.
So your client is doing it wrong, you should create the JSON there like
let body = JSON.stringify(epic);
Just remove the superflous {} around 'epic'.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.