0

我正在尝试使用此休息方法从数据库中接收用户对象:

@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");
    }
});

有谁知道我做错了什么?

4

1 回答 1

1

我认为这是在制造问题。

data: JSON.stringify({ User: user })

您应该将其更改为:

data: { User:JSON.stringify(user) }

希望能帮助到你。

于 2016-01-20T13:24:27.937 回答