所以我有两门课
餐厅
@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);
}
现在我可以毫无问题地传递参数来添加 Restful 服务的餐厅类
但我对餐厅详细信息类感到困惑。我如何将 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"
});
最佳答案
尝试将@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"
});
关于java - 如何使用 Jquery 将多个对象作为表单参数传递给 Restful 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20913975/