问题
I have a little problem between my RestController response and my AngularJs Controller.
First, I'm calling my rest webservice with $http :
myModule.controller('UserDetailsCtrl',function($http, idUser){
var self = this;
$http.post('/get-user-details', idUser).then(
function(response){
self.clients = response.data.usersClients;
for(client of self.clients){
console.log("pkUsersClients : " + client.pkUsersClients); // console display "[object Object]"
console.log("client : " + client.pkUsersClients.client); // console display "1" instead of User Object
console.log("keys : " + Object.keys(client.pkUsersClients)); // console display "keys : user,client"
console.log("name : " + client.pkUsersClients.client.name); // display "undefined"
}
}, function(){
// error
}
);
});
Then, I have my spring @RestController :
@RestController
public class UserDetailsRest {
@Autowired
private UserDao userDao;
@Autowired
private UserDatasDao userDatasDao;
@Autowired
private UsersClientsDao usersClientsDao;
@RequestMapping("get-user-details")
public UserDetailsDto getUserDetails(@RequestBody Long idUser){
User user = userDao.findByIdUser(idUser);
UserDatas userDatas = userDatasDao.findByIdUser(user);
List<UsersClients> usersClients = usersClientsDao.findByPkUsersClientsUser(user);
UserDetailsDto userDetailsDto = new UserDetailsDto(userDatas, usersClients);
return userDetailsDto;
// userDetailsDto contains : List<UsersClients> (2 lines). In each line, UsersClients contains : UserClientId pkUsersClients. Which contains : User + Client.
// But in Angular, User and Client are int and int (the id in fact).
}
}
So, my issue is that :
my webservice return an object UserDetailsDto which contains:
private List usersClients;
each UsersClients in this List contains :
private List usersClients;
private UserClientId pkUsersClients
pkUsersClients is a @Embeddable Object which contains :
private User user;
private Client client;
in angularjs, User and Client are not Object but int.
My classes :
my DTO which is the response
public class UserDetailsDto { private List<UsersClients> usersClients; public UserDetailsDto(List<UsersClients> _usersClients) { super(); this.usersClients = _usersClients; } // getters/setters // toString override }
My association table :
@Entity @Table(name="users_clients") @AssociationOverrides( { @AssociationOverride( name="pkUsersClients.user", joinColumns = @JoinColumn(name="id_user") ), @AssociationOverride( name="pkUsersClients.client", joinColumns = @JoinColumn(name="id_client") ) } ) public class UsersClients { private UserClientId pkUsersClients = new UserClientId(); @Temporal(TemporalType.DATE) private Date date_begin; @EmbeddedId public UserClientId getPkUsersClients() { return pkUsersClients; } public void setPkUsersClients(UserClientId pkUsersClients) { this.pkUsersClients = pkUsersClients; } @Transient public User getUser() { return getPkUsersClients().getUser(); } public void setUser(User user) { getPkUsersClients().setUser(user); } @Transient public Client getIdClient() { return getPkUsersClients().getClient(); } public void setIdClient(Client client) { getPkUsersClients().setClient(client); } public Date getDate_begin() { return date_begin; } public void setDate_begin(Date date_begin) { this.date_begin = date_begin; } }
to finish, my embeddable class :
@Embeddable public class UserClientId implements Serializable{ private static final long serialVersionUID = 1L; private User user; private Client client; /** * @return the user */ @ManyToOne(cascade=CascadeType.ALL) public User getUser() { return user; } /** * @param user the user to set */ public void setUser(User user) { this.user = user; } /** * @return the client */ @ManyToOne(cascade=CascadeType.ALL) public Client getClient() { return client; } /** * @param client the client to set */ public void setClient(Client client) { this.client = client; } }
EDIT : With chrome developper tools, I can see that client = "1" BUT user is an object. Now, the problem is that my response contains :
{
"avatar": null,
"identity": "M. edith USER",
"login": "EditUser",
"address": "125, rue du test. testLand (testLand)",
"email": "john.doe@gmail.com",
"idUser": 31,
"usersClients": [
{
"pkUsersClients": {
"user": {
"idUser": 31,
"login": "EditUser",
"pwd": "$2a$10$7G/GWrd5e3d3SVkZL5.EbOwbXP7NABCVxTPJVRZ0AVJYMtP/bgx.K",
"locked": "F",
"avatar": null,
"usersClients": [
{
"pkUsersClients": {
"user": 31,
"client": {
"idClient": 5,
"name": "BBBBBBBB",
"description": "TEST",
"logo": "iVBORw0KGgoAAAANSUhEUgAAAG..."
}
},
"date_begin": 1464818400000,
"date_end": 1467237600000,
"enable": false,
"user": 31,
"idClient": 5
},
{
"pkUsersClients": {
"user": 31,
"client": {
"idClient": 1,
"name": "AAAAAA",
"description": "La société Inside Group...",
"logo": "/9j/4QDCRXhpZgAASUkqAAgAAAAHABIBAwABA..."
}
},
"date_begin": null,
"date_end": null,
"enable": false,
"user": 31,
"idClient": 1
}
]
},
"client": 1
},
"date_begin": null,
"date_end": null,
"enable": false,
"user": 31,
"idClient": 1
},
{
"pkUsersClients": {
"user": 31,
"client": 1
},
"date_begin": null,
"date_end": null,
"enable": false,
"user": 31,
"idClient": 1
},
{
"pkUsersClients": {
"user": 31,
"client": 5
},
"date_begin": 1464818400000,
"date_end": 1467237600000,
"enable": false,
"user": 31,
"idClient": 5
}
]
You can see that it is a little disturbing :/
Please, give me some help, I don't understand where I make a mistake. Thanks in advance.
来源:https://stackoverflow.com/questions/37591485/spring-jpa-embeddable-class-send-int-instead-of-object