【问题标题】:Creating a pojo for springboot microservice为 Spring Boot 微服务创建 pojo
【发布时间】:2019-12-04 18:47:40
【问题描述】:

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

【问题讨论】:

  • 您可以通过邮递员发送"intArrayName" : [111,222,333], "stringArrayName" : ["a","b","c"]

标签: java spring-boot microservices


【解决方案1】:

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

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

【讨论】:

  • 在mongoDb中,notes栏写No field.Other columns are working
【解决方案2】:

"recipes":5,6,7 将导致错误,因为配方的类型为 List。您可以通过邮递员传递列表,如下所示

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

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

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

【讨论】:

  • 在mongoDb中,notes栏写No field.Other columns are working
  • 你好@sneijder10。您能否在控制台中更新完整错误的问题
  • 你写了我无法确定这种类型(一对多关系)所以你使用了@OneToMany关系还是你在谈论List
  • 你好。实际上没有错误。我用邮递员发送请求。它返回 200 好的。但是我可以看到表格上没有备注列的字段。
  • 非常感谢。它正在工作。邮递员中的属性名称不同。
猜你喜欢
  • 1970-01-01
  • 2021-04-09
  • 2018-04-18
  • 2022-01-03
  • 2020-03-11
  • 2022-01-08
  • 2020-09-27
  • 2019-02-20
  • 2020-12-09
相关资源
最近更新 更多