Spring MVC: exception when using @ResponseBody and circular link object

I have the following method, which, behind the scenes, uses Jackson to parse a list of objects in json:

@Controller @RequestMapping("/user/") public class EditarLugarController { @RequestMapping(value = "stores/{id}/branches", method = RequestMethod.GET) public @ResponseBody List<Branch> renderBranchesPerStore(@PathVariable(value = "id") Integer id) { if(branches == null) { //get branches based on store id } return branches; } 

This method is called from view via ajax using jquery

 var idBranch = '${store.id}'; $.get("http://localhost:8080/myapp/user/stores/" + idBranch+ "/branches", function(data) { // show json objects in page }); 

The problem is that when this method ends, it throws the following exception:

 java.lang.IllegalStateException: getOutputStream() has already been called for this response at org.apache.catalina.connector.Response.getWriter(Response.java:639) 

I tried removing @PathVariable (returning some default value) and it works fine, but I need this parameter. Is there a problem with this problem?

EDIT : here the branch code is as requested, but again: I DO NOT think this is due to the problem, because if I do not use @PathVariable, it works fine, the list of branches is processed normally by json and sent for viewing. In addition, I use the Jackson plugin for Hibernate, which tells Jackson not to parse attributes that are lazy loaded to throw an exception

 @Entity @Table(name = "BRANCH") public class Branch implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name="BRANCH_ID") @GeneratedValue(strategy= GenerationType.TABLE) private Integer id; @Column(name = "DESCRIPTION") @Size(max = 500) private String description; @Column(name="STREET") @NotNull @Size(max = 100) private String street; @Column(name="NUMBER") @NotNull @Size(max = 6) private String number @Column(name="FLOOR") @Size(max = 3) private String floor; @Column(name="APT") @Size(max = 10) private String apt @OneToMany(cascade = CascadeType.ALL, mappedBy="branch") private List<BranchPhoto> photos; @JoinColumn(name = "STORE_FK", referencedColumnName = "STORE_ID") @ManyToOne(optional = false) private Store store; public Branch() {} // getters & setters 

}

EDIT: I realized that even without PathVariable this is the same exception, I had to check it wrong. So the problem is actually related to circular reference when parsing json

+4
source share
3 answers

Does the branch include irreconcilable?

Serializing this function is the culprit ... in particular, circular reference.

Usually I create a form-specific dto and summarize the serialization.

+3
source

As Nimhimsky said, the problem was caused for circular references in the model, in the Branch class. Besides using dtos to simplify serialization, another solution for those who don't want to use DTO (like me) is to use some annotations to tell Jackson how to handle attributes. these are the required annotations:

In the Store class, the branch attribute:

 @OneToMany(mappedBy = "store", cascade = CascadeType.ALL) @JsonManagedReference // this annotation prevents the exception private List<Branch> branches 

And in the Branch class, in the attribute store:

 @JoinColumn(name = "LUGAR_FK", referencedColumnName = "LUGAR_ID") @ManyToOne(optional = false) @JsonBackReference // this annotation prevents the exception private Lugar lugar; 
+4
source

I know this was given some time ago, but it helped me find a workaround.

To exclude circular references to a value that I don't need so much. I created an inner class with only the fields I needed (none of them were referenced by one or several to one / many) and wrote an internal private class ResponseBody

0
source

Source: https://habr.com/ru/post/1487893/


All Articles