嵌套两个弹簧模型

时间:2019-01-26 13:58:24

标签: java spring-boot model nested

我尝试嵌套两个模型,所以我的响应应如下所示:

{
    "name": "some_name",
    "value": ["value1", "value2"],
    "details": {
        "name": "Test name",
        "surname": "Test surname",
        "phone": "Test phone",
        "email": "Test email"
    }
}

我有有效的CRUD MongoDB API,但此处不正确。我在控制器中没有“详细信息”。

对象:com.user.model.User@7fe8380d [_id = null,名称= some_name,值= [值1,值2],详细信息=空]

让我告诉你到目前为止我做了什么:

控制器:

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import org.apache.commons.lang3.builder.ToStringBuilder;

@RestController
@RequestMapping("/someaction")
public class UserController {

    private final UserService service;

    public UserController(UserService service) {
        this.service = service;
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public User createUser(@Valid @RequestBody User user) {
        System.out.println("An object: " + ToStringBuilder.reflectionToString(user));
        return service.createUser(user);
    }

}

服务:

import com.inpost.user_registration.repository.UserRegistrationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.bson.types.ObjectId;

@Service
public class UserService {

    @Autowired
    private UserRepository repository;

    public User createUser(User user) {
        user.set_id(ObjectId.get());
        repository.save(user);
        return user;
    }

}

存储库:

import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface UserRepository extends MongoRepository<User, String> {
    User findBy_id(ObjectId _id);
}

“详细信息”的模型:

public class Details {
    private String name;
    private String surname;
    private String phone;
    private String email;

    public Details() {
    }

    public Details(String name, String surname, String phone, String email) {
        this.name = name;
        this.surname = surname;
        this.phone = phone;
        this.email = email;
   }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Details [_id=" + name + ", env=" + surname + ", services=" + phone + ", email=" + email + "]";
    }

}

“用户”的模型:

import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import java.util.ArrayList;

public class User {

    @Id
    public ObjectId _id;
    public String name;
    public ArrayList<String> value;
    public Details details;

    public User() {}

    public User(ObjectId _id, String name, ArrayList<String> value, Details details) {
        this._id = _id;
        this.name = name;
        this.value = value;
        this.details = details;
    }

    public String get_id() {
        return _id.toHexString();
    }

    public void set_id(ObjectId _id) {
        this._id = _id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ArrayList<String> getValue() {
        return value;
    }

    public void setValue(ArrayList<String> value) {
        this.value = value;
    }

    public Details getDetails() {
        return details;
    }

    public void setDetails(Details details) {
        this.details = details;
    }

    @Override
    public String toString() {
        return "User [_id=" + _id + ", name=" + name + ", value=" + value + ", details=" + details + "]";
    }

}

现在,当我尝试保存“名称”和“值”时,一切正常。 当我尝试发布带有“详细信息”的帖子时,我会得到null

{
    "_id": "5c4c5c3f7a56c12578122d73",
    "name": "some_name",
    "value": [
        "value1",
        "value2"
    ],
    "details": null
}

有人可以帮助我吗?在某个地方,我跳过了一些重要的事情。

1 个答案:

答案 0 :(得分:0)

我认为您需要专门在Details实体上调用save或使用CascadeType.ALL对其进行注释,以在持久保留User实体时完成Details实体的自动持久。