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) {
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) {
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() {}
}
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