1

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
}

1 Answer 1

3

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'.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.