0

我的 RestController 响应和我的 AngularJs 控制器之间有一个小问题。

首先,我用 $http 调用我的 rest 网络服务:

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

});

然后,我有我的弹簧 @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).

    }

}

所以,我的问题是:

  • 我的网络服务返回一个对象 UserDetailsDto ,其中包含:

    私有列表 usersClients;

  • 此列表中的每个 UsersClients 包含:

    私有列表 usersClients;

    私人 UserClientId pkUsersClients

  • pkUsersClients 是一个 @Embeddable 对象,其中包含:

    私人用户用户;

    私人客户客户;

  • 在 angularjs 中,User 和 Client 不是 Object 而是 int。

我的课程 :

  • 我的 DTO 是响应

    public class UserDetailsDto {
    
        private List<UsersClients> usersClients;
    
        public UserDetailsDto(List<UsersClients> _usersClients) {
            super();
            this.usersClients = _usersClients;
        }
    
        // getters/setters
        // toString override
    
    }
    
  • 我的关联表:

    @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;
        }
    
    }
    
  • 最后,我的可嵌入类:

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

编辑:使用 chrome 开发工具,我可以看到 client = "1" 但 user 是一个对象。现在,问题是我的回复包含:

{
  "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
    }
  ]

你可以看到这有点令人不安:/

请给我一些帮助,我不明白我在哪里犯了错误。提前致谢。

4

0 回答 0