为springboot微服务创建一个pojo

时间:2019-07-26 12:08:45

标签: java spring-boot microservices

我想在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+
                '}';
    }
}

2 个答案:

答案 0 :(得分:0)

在配方中添加[]后可以使用。

<html>    
  <head> 

    <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/core.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/md5.js"></script>

  <script>
     var hash = CryptoJS.MD5("Message");
     console.log(hash);
  </script>

  </head>
  <body>

  </body>
</html>

答案 1 :(得分:0)

"recipes":5,6,7将导致错误,因为配方的类型为List。您可以按以下方式通过邮递员传递清单

  • 用于列表 "intArrayName" : [111,222,333]
  • 用于字符串列表 "stringArrayName" : ["a","b","c"]

对于上述UseCase,您可以将其发送为

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