我创建了一个控制器来将新用户添加到数据库中 但是当我在邮递员中测试Post请求时我得到了这个错误:
{“code”:null,“message”:“缺少必需的请求正文:public com.pack.model.persistent.User com.pack.restapi.controllers.UserController.create(com.model.persistent.User)“}
这是做了什么:
//User class
@Entity
public class User extends PersistenceGenerique {
private static final long serialVersionUID = 245457152874587458468L;
@Column
@OrderBy
private String firstname;
@Column
private String lastname;
@Column
private String email;
//service
public User create(User user);
//serviceIMPL
@Override
public User create(User user) {
userDao.persist(user);
return user;
}
//RestController
@PostMapping
public User create(@RequestBody User user){
return userService.create(user);
}
以下是我的打字稿代码以获取更多详细信息:
createUser(): void {
this.usrSRV.CreateUser(this.user)
.subscribe(data => {
alert("User created successfully.");
});
};
CreateUser(user) {
const userUrl = this._userUrl;
return this.http.post(userUrl, user)
.map(res => {
return res.json();
})
.catch(this.handleError);
}
<form (ngSubmit)="createUser()">
<div class="form-group">
<label for="firstname">Firstname</label>
<input type="text" [(ngModel)]="firstname" placeholder="Firstname" name="firstname" class="form-control" id="firstname">
</div>
<div class="form-group">
<label for="lastname">LastName</label>
<input [(ngModel)]="lastname" placeholder="lastname" name="lastname" class="form-control" id="lastname">
</div>
<div class="form-group">
<label for="email">email</label>
<input [(ngModel)]="email" placeholder="email" name="email" class="form-control" id="email">
</div>
<button class="btn btn-success">Create user</button>
</form>
</div>