我有一个场景,我通过服务器的AJAX响应填充JSP中的下拉框。在控制器中,我正在重新调整Product对象的集合,并使用@ResponseBody注释了返回类型。
控制器 -
@RequestMapping(value="/getServicesForMarket", method = RequestMethod.GET)
public @ResponseBody Collection<Product> getServices(@RequestParam(value="marketId", required=true) int marketId) {
Collection<Product> products = marketService.getProducts(marketId);
return products;
}
产品
@Entity
@Table(name = "PRODUCT")
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private Market market;
private Service service;
private int price;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "MARKET_ID")
public Market getMarket() {
return market;
}
public void setMarket(Market market) {
this.market = market;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "SERVICE_ID")
public Service getService() {
return service;
}
public void setService(Service service) {
this.service = service;
}
@Column(name = "PRICE")
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
服务
@Entity
@Table(name="SERVICE")
public class Service implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private String name;
private String description;
@Id
@GeneratedValue
@Column(name="ID")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="DESCRIPTION")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
在JSP中,我还需要从Product的服务字段中获取数据。所以我在我的JQuery回调函数中,我写了类似product.service.description来获取数据。
默认情况下,Jackson似乎没有映射关联的服务对象(或任何其他自定义对象)。我也没有任何例外。在JSP中,我没有得到数据。当我返回一些不包含任何其他自定义对象作为其字段的对象的集合时,它工作正常。
我是否遗漏了任何可以使用的设置?
谢谢!