Spring MVC使用Jackson返回ajax响应 [英] Spring MVC return ajax response using Jackson
问题描述
我有一个场景,我通过服务器的AJAX响应填充JSP中的下拉框。在控制器中,我正在重新调整Product对象的集合,并使用@ResponseBody注释返回类型。
I have a scenario where I am filling a dropdown box in JSP through AJAX response from the server. In the controller, I am retuning a Collection of Product objects and have annotated the return type with @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中,我需要从中获取数据产品的服务领域也。所以我在我的JQuery回调函数中,我编写了类似product.service.description的数据来获取数据。
In the JSP, I need to get the data from the service field of Product also. So I in my JQuery callback function, I have written like product.service.description to get the data.
默认情况下Jackson似乎没有映射关联的服务对象(或任何其他自定义对象)。我也没有任何例外。在JSP中,我没有得到数据。当我返回一些不包含任何其他自定义对象作为其字段的对象的集合时,它工作正常。
It seems that by default Jackson is not mapping the associated service object (or any other custom object). Also I am not getting any exception. In the JSP, I do not get the data. It is working fine when I return Collection of some object which does not contain any other custom objects as its fields.
我是否遗漏了任何可以使用的设置?
Am I missing any settings for this to work?
谢谢!
推荐答案
我建议使用hibernate模块 jackson ,这将帮助您忽略hibernate未启动的对象。这样你就不会有异常,只有完全初始化的转换。
i would suggest to use hibernate module for jackson, this will help you to ignore hibernate un-initlized objects. this way you will not have exception and only fully initilzed conversion.
这篇关于Spring MVC使用Jackson返回ajax响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!