0

我有一個模型稱爲自行車,我可以插入,獲取,列出對象。但現在,我只想更新谷歌應用引擎上的自行車對象的價格,保留所有剩餘的字段。所以我已經通過補丁方法。我沒有得到如何在Google應用引擎端點中使用補丁方法來更新價格。如何在谷歌應用程序引擎端點使用補丁方法

這是我的自行車模型

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

在類似的方式,我想用打補丁的方法只更新自行車的價格。

回答

0

我不認爲您可以在Google Cloud Endpoints中使用PATCH HTTP方法,請參閱文檔@ApiMethod Annotation,其中指出「以實體作爲參數的方法應該使用HttpMethod.POST(用於插入操作)或HttpMethod.PUT(用於更新操作)「(https://cloud.google.com/appengine/docs/java/endpoints/annotations)。

https://cloud.google.com/appengine/docs/java/endpoints/javadoc/com/google/api/server/spi/config/ApiMethod.HttpMethod

見,如果你想避免發送一個完整的「自行車」的資源表示(例如,使用更少的帶寬)是創建一個特定的類(標爲@Entity),你可以做些什麼,只有有兩個必要的領域。讓我們把它BikePrice例如

@Entity 
public class BikePrice { 

    @Id 
    protected Long id; 

    protected String price; 

然後創建一個專用的端點方法(用BikePrice實體參數)中,你通過物化加載你的自行車原始實體和更新

.... 
    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之間的一種「容器」或「數據傳送帶」。