我有一个场景,我通过来自服务器的 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 来获取数据。
似乎默认情况下杰克逊没有映射关联的服务对象(或任何其他自定义对象)。我也没有任何例外。在 JSP 中,我没有得到数据。当我返回某个不包含任何其他自定义对象作为其字段的对象的集合时,它工作正常。
我是否缺少任何设置以使其正常工作?
谢谢!