Spring MVC return ajax response using Jackson

2019-08-17 09:59发布

问题:

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.

Controller -

@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;
}

And Product is

@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;
    }
}

Service is

@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;
    }
}

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.

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?

Thanks!

回答1:

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.