如何使用Jquery将多个对象作为表单参数传递给Restful Service

时间:2014-01-03 22:35:36

标签: java jquery rest post hql

所以我有两个班级

餐厅

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name = "resturant")
public class Restaurant {
private int id;
private String name;
private String Location;

private int rating;

private float longitude;

private float latitude;

public Restaurant(String name, String location, int rating, float longi,
        float lati) {
    this.name = name;
    this.Location = location;
    this.rating = rating;
    this.longitude = longi;
    this.latitude = lati;
}

public Restaurant() {

}

@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
@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 = "Location")
public String getLocation() {
    return Location;
}

public void setLocation(String location) {
    Location = location;
}

@Column(name = "rating")
public int getRating() {
    return rating;
}

public void setRating(int rating) {
    this.rating = rating;
}

@Column(name = "longitutde")
public float getLongitude() {
    return longitude;
}

public void setLongitude(float longitude) {
    this.longitude = longitude;
}

@Column(name = "latitude")
public float getLatitude() {
    return latitude;
}

public void setLatitude(float latitude) {
    this.latitude = latitude;
}

}

和 型

@Entity
@Table(name = "type")
public class Type {
private int id;
private String name;

public Type() {

}

public Type(String name) {
    this.name = name;

}

@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
@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;
}

}

现在我有了第三个类,它将这两个类的对象作为外键

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name = "restaurant_detail")
public class RestaurantDetails {

private transient Restaurant restaurant;
private transient Type type;
private int id;


public RestaurantDetails() {

}
public RestaurantDetails(Restaurant r , Type t){
    this.restaurant=r;
    this.type=t;
}

@ManyToOne
@JoinColumn(name = "restaurant_id")
public Restaurant getRestaurant() {
    return restaurant;
}
public void setRestaurant(Restaurant restaurant) {
    this.restaurant = restaurant;
}

@ManyToOne
@JoinColumn(name = "restaurant_type")
public Type getType() {
    return type;
}
public void setType(Type type) {
    this.type = type;
}

@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
@Column(name = "id")
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}



}

现在我已经设置了一个将餐厅添加到数据库的宁静服务

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void addRestaurant(@FormParam("name") String name,
        @FormParam("location") String location,
        @FormParam("longitutde") float longi,
        @FormParam("latitude") float lati, @FormParam("rating") int rating) {
    Restaurant r = new Restaurant(name, location, rating, longi, lati);
    RestaurantBO rBo = new RestaurantBO();
    rBo.addRestaurant(r);

}

和一个添加餐厅详细信息

    @Path("/restaurantDetail")
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void addRestaurantDetail(@FormParam("restaurant") Restaurant restaurant,
        @FormParam("type") Type type) {
    RestaurantDetails r = new RestaurantDetails(restaurant,type);
    RestaurantBO rBo = new RestaurantBO();
    rBo.addRestaurantDetails(r);

}

现在我没有问题传递参数来添加宁静服务的餐厅类

但我对餐厅细节课感到困惑。如何将2个类对象作为参数传递给服务。如何构造JSON以传递数据或表单参数

我使用这种简单的方式来调用服务

$.ajax({
        type : "POST",
        url : "http://localhost:8080/Appetizers_project/rest/restuarant",
        data : data,
        success : function() {

            alert();
        },
        error : function() {
            alert("ERROR Occured");
        },
        dataType : "text"
    });

1 个答案:

答案 0 :(得分:1)

尝试将@consume更改为Application_Json并直接读取RestaurantDetails对象

@Path("/restaurantDetail")
@POST
@Consumes(MediaType.APPLICATION_JSON)
public void addRestaurantDetail(RestaurantDetails restDet) {

    rBo.addRestaurantDetails(restDet);

}

在客户端使用JSON.stringify库

data = {
   restaurant:{
      //rest props
   },
   type: {
      //type props
   },
};

$.ajax({
        type : "POST",
        url : "http://localhost:8080/Appetizers_project/rest/restuarant",
        data : JSON.stringify(data),
        success : function() {

            alert();
        },
        error : function() {
            alert("ERROR Occured");
        },
        dataType : "json"
    });