【问题标题】:How to use patch method in google app engine endpoints如何在谷歌应用引擎端点中使用补丁方法
【发布时间】:2017-02-15 04:44:26
【问题描述】:

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

这是我的自行车模型

@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 的价格。

【问题讨论】:

    标签: android google-app-engine backend google-cloud-endpoints


    【解决方案1】:

    我认为您不能将 PATCH HTTP 方法与 Google Cloud Endpoints 一起使用,请参阅@ApiMethod Annotation 的文档,其中说“将实体作为参数的方法应使用 HttpMethod.POST(用于插入操作)或 HttpMethod .PUT(用于更新操作)”(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

    如果您想避免发送完整的“自行车”资源表示(例如使用较少的带宽),您可以做的是创建一个特定的类(注释为 @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 之间的一种“容器”或“数据传送器”。

    【讨论】:

    • 我刚刚更新了框架 2 的链接并进行了检查。即使他们似乎不再明确这么说,该类也不包含 PATCH 作为可能的枚举,并且在调用 PATCH 时,客户端会收到 404。
    猜你喜欢
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多