[英]How to use patch method in google app engine endpoints
我有一個名為Bike的模型,我能夠插入,獲取,列出對象。 但是現在我只想更新Google應用程序引擎上Bike Object的價格,並保持所有剩余字段不變。 所以我經歷了補丁方法。 我沒有得到如何在Google App Engine端點中使用補丁方法來僅更新價格。
這是我的自行車模特
@Entity
公共課自行車{
@Id
protected Long id;
@Index
protected String imageUrl;
@Index
protected String title;
@Index
protected String price;
@Index
protected String kmpl;
@Index
protected String cc;
@Index
protected String make;
public Bike(){}
public Bike(String s, String s1, String s2,
String s3, String s4,String s5) {
this.title = s;
this.cc = s1;
this.kmpl = s2;
this.price = s3;
this.imageUrl=s4;
this.make=s5;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getKmpl() {
return kmpl;
}
public void setKmpl(String kmpl) {
this.kmpl = kmpl;
}
public String getCc() {
return cc;
}
public void setCc(String cc) {
this.cc = cc;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
}
這是我的插入api
/**
* Inserts a new {@code Bike}.
*/
@ApiMethod(
name = "insert",
path = "bike",
httpMethod = ApiMethod.HttpMethod.POST)
public Bike insert(Bike bike) {
// Typically in a RESTful API a POST does not have a known ID (assuming the ID is used in the resource path).
// You should validate that bike.id has not been set. If the ID type is not supported by the
// Objectify ID generator, e.g. long or String, then you should generate the unique ID yourself prior to saving.
//
// If your client provides the ID then you should probably use PUT instead.
ofy().save().entity(bike).now();
logger.info("Created Bike with ID: " + bike.getId());
return ofy().load().entity(bike).now();
}
以類似的方式,我想使用補丁方法僅更新Bike的價格。
我認為您不能將PATCH HTTP方法與Google Cloud Endpoints一起使用,請參閱@ApiMethod Annotation的文檔,其中說:“將實體作為參數的方法應該使用HttpMethod.POST(用於插入操作)或HttpMethod.PUT(以進行更新操作)”( https://cloud.google.com/endpoints/docs/frameworks/java/annotations )。
如果要避免發送完整的“自行車”資源表示(例如,使用較少的帶寬),該怎么做是創建一個只有兩個必填字段的特定類(標注為@Entity)。 例如,我們稱它為BikePrice
@Entity
public class BikePrice {
@Id
protected Long id;
protected String price;
然后,您創建一個專用的終結點方法(以BikePrice實體作為參數),在其中通過Objectify加載Bike原始實體並對其進行更新。
....
httpMethod = ApiMethod.HttpMethod.PUT)
public void updateBike(BikePrice bikePrice) {
Bike b = ofy().load().type(Bike.class).id(bikePrice.getId()).now();
b.setPrice(bikePrice.getPrice());
....
請注意,您永遠不會在數據存儲區中保存任何BikePrice實體。 它只是用作前端和App Engine之間的一種“容器”或“數據傳送器”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.