Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring:json不显示重复的OneToMany实体_Json_Spring_Hibernate - Fatal编程技术网

Spring:json不显示重复的OneToMany实体

Spring:json不显示重复的OneToMany实体,json,spring,hibernate,Json,Spring,Hibernate,我花了半天时间在这上面,但我没有找到答案。当我在Postman中发送GET请求以获取所有实体时,它不会给我重复的值。这是我的密码 @Entity @Table(name = "SERVICE") public class Service { @Id @Column(name = "ID") @GeneratedValue(strategy = GenerationType.AUTO) @JsonView(View.OnlyService.class) p

我花了半天时间在这上面,但我没有找到答案。当我在Postman中发送GET请求以获取所有实体时,它不会给我重复的值。这是我的密码

@Entity
@Table(name = "SERVICE")
public class Service {

    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    @JsonView(View.OnlyService.class)
    private Long id;

    @Column(name = "SERVICE_NAME", length = 50, unique = true)
    @NotNull
    @JsonView({View.OnlyService.class, View.OnlyCategory.class})
    private String serviceName;

    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = ServerRequest.class)
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "CATEGORY_ID", referencedColumnName = "ID")
    @JsonView(View.OnlyService.class)
    private Category category;

    // getters and setters
}

而不是

[
{
    "id": 1,
    "serviceName": "Jon's Pharmacy",
    "category": {
        "name": "PHARMACY"
    }
},
{
    "id": 2,
    "serviceName": "Adam's shop",
    "category": {
        "name": "SHOP"
    }
},
{
    "id": 3,
    "serviceName": "Judd's pharmacy",
    "category": {
        "name": "PHARMACY"
    }
},
{
    "id": 4,
    "serviceName": "Adam's pharmacy",
    "category": {
        "name": "PHARMACY"
    }
}
]
如何强制去做呢


请原谅我的所有错误-这是我的第一个问题:)

因为您使用了JsonIdentityInfo,它就是这样做的,如文档所述::实际上,这是通过将第一个实例序列化为完整对象和对象标识,并将对该对象的其他引用序列化为引用值来完成的。谢谢。现在一切正常:)
@RestController
public class ServiceRestService {

    @Autowired
    ServiceRepository serviceRepository;
    @Autowired
    CategoryRepository categoryRepository;

    @JsonView(View.OnlyService.class)
    @RequestMapping(path = "/getAll", method = RequestMethod.GET)
    public Iterable<Service> getAllServices() {
        //return this.serviceRepository.findByCategoryId(1);
        return this.serviceRepository.findAll();
    }
}
[
{
    "id": 1,
    "serviceName": "Jon's Pharmacy",
    "category": {
        "name": "PHARMACY"
    }
},
{
    "id": 2,
    "serviceName": "Adam's shop",
    "category": {
        "name": "SHOP"
    }
},
{
    "id": 3,
    "serviceName": "Judd's pharmacy",
    "category": 1
},
{
    "id": 4,
    "serviceName": "Adam's pharmacy",
    "category": 1
}
]
[
{
    "id": 1,
    "serviceName": "Jon's Pharmacy",
    "category": {
        "name": "PHARMACY"
    }
},
{
    "id": 2,
    "serviceName": "Adam's shop",
    "category": {
        "name": "SHOP"
    }
},
{
    "id": 3,
    "serviceName": "Judd's pharmacy",
    "category": {
        "name": "PHARMACY"
    }
},
{
    "id": 4,
    "serviceName": "Adam's pharmacy",
    "category": {
        "name": "PHARMACY"
    }
}
]