JsonMappingException: Spring Boot的GET请求中出现无限递归(StackOverflowError)错误。

huangapple 未分类评论43阅读模式
英文:

JsonMappingException: Infinite recursion (StackOverflowError) in Spring Boot GET request

问题

@GetMapping("/machines")
public ResponseEntity<List<Machine>> getMachines() {
    List<Machine> result = this.machineRepository.findAll();
    return new ResponseEntity<>(result, HttpStatus.OK);
}
@Entity
public class Machine {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;

    private String name;

    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
    @JoinTable(
        name = "software_codes",
        joinColumns = @JoinColumn(name = "machine_id"),
        inverseJoinColumns = @JoinColumn(name = "software_id"))
    private Set<SoftwareCode> softwareCodes = new HashSet<>();

    public Machine(String name, Set<SoftwareCode> softwareCodes) {
        this.name = name;
        this.softwareCodes = softwareCodes;
    }
}

@Entity
public class SoftwareCode {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;

    private String code;

    @ManyToMany(mappedBy = "softwareCodes", cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
    private Set<Machine> machines;

    public SoftwareCode(String code) {
        this.code = code;
    }
}
Set<Machine> machines = new HashSet<>();
Set<SoftwareCode> softwareCodes = new HashSet<>();

SoftwareCode softwareCode = new SoftwareCode("ABC");
SoftwareCode softwareCode2 = new SoftwareCode("XYZ");
softwareCodes.add(softwareCode);
softwareCodes.add(softwareCode2);
Machine machine = new Machine("machine1", softwareCodes);
machines.add(machine);

vehicleRepository.saveAll(machines);
英文:

I'm using Spring Boot to create a restful API along with JPA. I'm having problems when trying to get results using GET request with ResponseEntity to generate a JSON. Looks like it's returning an infinite JSON..

Code:

    @GetMapping(&quot;/machines&quot;)
    public ResponseEntity&lt;List&lt;Machine&gt;&gt; getMachines() {
        List&lt;Machine&gt; result = this.machineRepository.findAll();
        return new ResponseEntity&lt;&gt;(result, HttpStatus.OK);

Error:

[nio-8080-exec-1] s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request [/main/machines] and exception [Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: org.hibernate.collection.internal.PersistentSet[0]-&gt;com.main.example.model.SoftwareCode[&quot;machines&quot;]-&gt;org.hibernate.collection.internal.PersistentSet[0]-&gt;com.main.example.model.Machine[&quot;softwareCodes&quot;]-&gt;org.hibernate.collection.internal.PersistentSet[0]-&gt;com.

- Also, here's some background of what I persisted to DB, to give you some context

Machine.java:

@Entity
public class Machine {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;

    private String name;

    @ManyToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE}, fetch = FetchType.EAGER)
    @JoinTable(
        name = &quot;software_codes&quot;,
        joinColumns = @JoinColumn(name = &quot;machine_id&quot;),
        inverseJoinColumns = @JoinColumn(name = &quot;software_id&quot;))
    private Set&lt;SoftwareCode&gt; softwareCodes = new HashSet&lt;&gt;();

    public Machine(String name, Set&lt;SoftwareCode&gt; softwareCodes) {
        this.name = name;
        this.softwareCodes = softwareCodes;
    }

SoftwareCode.java:

@Entity
public class SoftwareCode {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;

    private String code;

    @ManyToMany(mappedBy = &quot;softwareCodes&quot;, cascade = {CascadeType.PERSIST,CascadeType.MERGE}, fetch = FetchType.EAGER)
    private Set&lt;Machine&gt; machines;

    public SoftwareCode(String code) {
        this.code = code;
    }

My approach to persist to DB:

Set&lt;Machine&gt; machines = new HashSet&lt;&gt;();
Set&lt;SoftwareCode&gt; softwareCodes = new HashSet&lt;&gt;();

SoftwareCode softwareCode = new SoftwareCode(&quot;ABC&quot;);
SoftwareCode softwareCode2 = new SoftwareCode(&quot;XYZ&quot;);
softwareCodes.add(softwareCode);
softwareCodes.add(softwareCode2);
Machine machine = new Machine(&quot;machine1&quot;, softwareCodes);
machines.add(machine);

vehicleRepository.saveAll(machines);

huangapple
  • 本文由 发表于 2020年5月5日 02:04:51
  • 转载请务必保留本文链接:https://java.coder-hub.com/61598744.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定