Java 使用Spring MVC生成JSON时发生堆栈溢出错误

Java 使用Spring MVC生成JSON时发生堆栈溢出错误,java,json,spring,spring-mvc,Java,Json,Spring,Spring Mvc,我正在尝试将Java对象转换为JSON并将其显示在网页上。现在的情况是,插头只打印一次,而细节和用户则被无限打印 下面是我试图打印它的方式 @RequestMapping(value = "/json/{userName}", method = RequestMethod.GET, produces = "application/json; charset=UTF-8") @ResponseBody() public List<Plugs> getPlugs(@PathVariabl

我正在尝试将Java对象转换为JSON并将其显示在网页上。现在的情况是,插头只打印一次,而细节和用户则被无限打印

下面是我试图打印它的方式

@RequestMapping(value = "/json/{userName}", method = RequestMethod.GET, produces = "application/json; charset=UTF-8")
@ResponseBody()
public List<Plugs> getPlugs(@PathVariable("userName") String userName) {
    User userInfo = userDAO.getUserInfo(userName);
    System.out.println("------Here's the thing------");
    System.out.println(userInfo.getDetails().getPlugs());
    return userInfo.getDetails().getPlugs();
}
--

我已经在私有字段和getter上尝试了
@JsonIgnore
@JsonBackReference
@JsonManagedReference
(但也许有人可以再次告诉我如何做)和
@JsonIdentityInfo
(也许有人也可以帮我做这件事)

问题:如何只打印插头而不打印JSON格式的其他内容


如果需要更多的代码,我将提供更多。

解决了问题。这里是我更改代码的地方

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "username")
@JsonIgnore
@XmlTransient
private User user
--

在每个实体的顶部,我把这个

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")

分享您的确切stacktrace。JPA与您的JSON生成有什么关系(即问题)?@NeilStockton Nothing。我只是说我在用JPA。@NeilStockton哦,我甚至没有意识到JPA被标记了。对不起
//Plugs.java
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_code", referencedColumnName = "post_code")
private Details details;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "username")
@JsonIgnore
@XmlTransient
private User user
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_code", referencedColumnName = "post_code")
@JsonIgnore
@XmlTransient
private Details details;
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")