我想在 Spring 启动中开发一个微服务应用程序。我创建了 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+
'}';
}
}
在
食谱中添加 [] 后它将起作用。
{
"id": 102,
"userName": "figen",
"email": 3,
"recipes":[5,6,7]
}
"recipes":5,6,7
会导致错误,因为配方的类型为 List
。您可以通过邮递员传递列表,如下所示
- 对于列表
"intArrayName" : [111,222,333]
- 对于字符串列表
"stringArrayName" : ["a","b","c"]
对于上述用例,您可以将其发送为
{
"id": 102,
"userName": "figen",
"email": 3,
"recipes":[5,6,7]
}