简体   繁体   English

Spring Boot-奇怪的JSON控制器响应

[英]Spring boot - Strange JSON Controller response

I have an application with SpringBoot. 我有一个SpringBoot应用程序。 I have a simple RestController: 我有一个简单的RestController:

@RestController
public class ClientController {
    private static final Logger logger = Logger.getLogger(ClientController.class);
    @Autowired ClientService clientService;

    @RequestMapping(value = "/client", method = RequestMethod.GET)
    public ResponseEntity<Client> getClient(@RequestParam(value = "idClient") int idClient)
         {
        Client client = clientService.findById(idClient);


        return new ResponseEntity<Client>(client, HttpStatus.OK);

    }

Client.java has 2 OneToMany fields, Luce and Posto, annotated in this way: Client.java有2个OneToMany字段Luce和Posto,用以下方式注释:

@OneToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL, mappedBy = "client")
    public List<Posto> getPosti() {
        return posti;
    }

    public void setPosti(List<Posto> posti) {
        this.posti = posti;
    }

    @OneToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL, mappedBy = "client")
    public List<Luce> getLuci() {
        return luci;
    }

    public void setLuci(List<Luce> luci) {
        this.luci = luci;
    }

When I try to call the url I'm getting a strange behavior for the response. 当我尝试调用url时,响应出现奇怪的行为。 Let's say I have 2 Posto and 2 Luci objects. 假设我有2个Posto和2个Luci对象。 Posto objects are linked to idClient=1 and only one Luce is linked to idClient=1. Posto对象链接到idClient = 1,只有一个Luce链接到idClient = 1。 So if i hit, for instance, http://localhost:8080/client?idClient=1 I should get 2 Posto and 1 Luce, but i get the following response (i deleted some not important field for brevity's sake): 因此,例如,如果我点击了http://localhost:8080/client?idClient=1我应该得到2个Posto和1个Luce,但是我得到以下响应(为简便起见,我删除了一些不重要的字段):

{
    "idClient": 1,

    "posti": [
        {
            "idPosto": 1,
            "numeroPosto": 61,
            "nomePosto": "Posto numero 61"
        },
        {
            "idPosto": 2,
            "numeroPosto": 152,
            "nomePosto": "Posto numero 62"
        }
    ],
    "luci": [
        {
            "idLuce": 1,
            "numeroLuce": 1,
            "nomeLuce": "Lampada 1",

        },
        {
            "idLuce": 1,
            "numeroLuce": 1,
            "nomeLuce": "Lampada 1",

        }
    ]
}

So I'm getting 2 times the same Luce object. 所以我得到了2个相同的Luce对象。 It happens also i the situation is inverted, 2 Luce and 1 Posto, i'm getting twice the only Posto. 如果情况倒转,也发生了这种情况:2个Luce和1个Posto,我得到的是唯一Posto的两倍。 If all the Posto and Luce objects are linked to idClient 1, the response is good, and is good also if I have no Luce (or no Posto) for an IdClient... I don't know where to look at, everything seems to work, i'm getting no errors... 如果所有Posto和Luce对象都链接到idClient 1,则响应很好,如果我没有IdClient的Luce(或Posto),响应也很好...我不知道从哪里看,一切似乎工作,我没有任何错误...

change list to set in Client.java class 更改列表以在Client.java类中设置

@OneToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL, mappedBy = "client")
public Set<Posto> getPosti() {
    return posti;
}

public void setPosti(Set<Posto> posti) {
    this.posti = posti;
}

@OneToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL, mappedBy = "client")
public Set<Luce> getLuci() {
    return luci;
}

public void setLuci(Set<Luce> luci) {
    this.luci = luci;
}

and implement Client.java class hascode() and equals() methods because Set object not take dublicate data 并实现Client.java类的hascode()和equals()方法,因为Set对象不会获取重复数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM