我正在尝试使用此休息方法从数据库中接收用户对象:
@RestController
@RequestMapping("/rest/users")
public class UserRestController {
@Autowired
private UserService service;
@RequestMapping(method = RequestMethod.GET)
public Collection<User> getAllUsers(@RequestBody(required = false) User user) throws BadHttpRequest {
System.out.println(user);//ALWAYS RETURNING NULL :(
if (user == null) {
return service.getAllUsers(); //Always returns this!
} else if (user.getUserId() == null) {
return Collections.singletonList(service.getUserByEmail(user.getUsername()));//I want this
} else if(user.getUsername() == null){
return Collections.singletonList(service.getUserById(user.getUserId()));
}else{
throw new BadHttpRequest(new Exception("User id/email mismatch"));
}
}
我使用这个 javascript:
var user = [{ "username": "fms7@hotmail.no", "userId": 1}];
$.ajax({
type: "get",
url: "/rest/users/",
data: JSON.stringify({ User: user }),
success: function(data){alert(data);},
error: function(errMsg) {
alert("bais");
}
});
有谁知道我做错了什么?