如何在 google 应用引擎端点中使用补丁方法

How to use patch method in google app engine endpoints

我有一个名为 Bike 的模型,我可以插入、获取、列出对象。但现在我只想更新 google 应用引擎上 Bike 对象的价格,保持所有剩余字段不变。所以我已经完成了补丁方法。我不知道如何使用 google 应用引擎端点中的补丁方法来仅更新价格。

这是我的自行车模型

@Entity

public class自行车{

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

以类似的方式我想使用补丁方法只更新自行车的价格。

我认为您不能将 PATCH HTTP 方法与 Google 云端点一起使用,请参阅 @ApiMethod 注释文档,其中显示 "Methods that take an entity as a parameter should use HttpMethod.POST (for insert operations) or HttpMethod.PUT (for update operations)" (https://cloud.google.com/endpoints/docs/frameworks/java/annotations)。

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

如果您想避免发送完整的 "Bike" 资源表示(例如,使用较少的带宽),您可以做的是创建一个特定的 class(注释为 @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 之间的一种 "container" 或 "conveyor of data"。