首页 > 解决方案 > 为 springboot 微服务创建 pojo

问题描述

我想在 Spring Boot 中开发一个微服务应用程序。我创建了 2 个服务,称为用户服务和配方服务。

我的问题是一个用户可以有多个食谱,但我无法确定食谱字段的类型。我不能使用private List<Recipe> recipes,因为我希望每个微服务都应该是独立的。你有什么主意吗?

如果我这样确定private List<Long> recipes如何向邮递员发送请求?

{
    "id": 102,
    "userName": "figen",
    "email": 3,
    "recipes":5,6,7     // line 5
}

由于第 5 行,此请求不起作用

import org.springframework.data.mongodb.core.mapping.Document;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

//@Entity
@Document(collection = "User")
public class User {

    @Id
    private String id;
    private String userName;

    private Long email;

    private List<Long> recipes; // I cannot determine this type(one-to-many relationship)

    public User(){

    }

    public User(String id, String userName, Long email,List<Long> recipes) {
        this.id = id;
        this.userName = userName;
        this.email = email;
        this.notes = recipes;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Long getEmail() {
        return email;
    }

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

    public List<Long> getRecipes() {
        return recipes;
    }

    public void setRecipes(List<Long> recipes) {
        this.notes = recipes;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", email='" + email + '\'' +
                ", recipes=" + recipes+
                '}';
    }
}

标签: javaspring-bootmicroservices

解决方案


在食谱中添加 [] 后它将起作用。

{
    "id": 102,
    "userName": "figen",
    "email": 3,
    "recipes":[5,6,7]  
}

推荐阅读