我有以下方法,它在幕后使用杰克逊将实体列表解析为 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's id
}
return branches;
}
使用 jquery 通过 ajax 从视图中调用此方法
var idBranch = '${store.id}';
$.get("http://localhost:8080/myapp/user/stores/" + idBranch+ "/branches",
function(data) {
// show json objects in page
});
问题是当这个方法结束时,它会抛出以下异常:
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:639)
我尝试删除@PathVariable(返回一些默认值),它工作正常,但我需要那个参数。这个问题有解决方法吗?
编辑:这是请求的分支代码,但同样:我不认为它与问题有关,因为如果我不使用 @PathVariable 它工作正常,分支列表被解析为 json 并发送到视图。另外,我正在为 Hibernate 使用 Jackson 插件,它告诉 Jackson 不要解析延迟加载的属性,以防止出现异常
@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
}
编辑:我已经意识到,即使没有 PathVariable 它也会抛出同样的异常,我一定是测试错了。所以问题实际上是解析json时的循环引用