简体   繁体   English

如何在Google App Engine端点中使用补丁方法

[英]How to use patch method in google app engine endpoints

I have a model called Bike, i am able to insert,get,list the object. 我有一个名为Bike的模型,我能够插入,获取,列出对象。 But now i want to update only the price of the Bike Object on the google app engine keeping all the remaining fields as it is. 但是现在我只想更新Google应用程序引擎上Bike Object的价格,并保持所有剩余字段不变。 So i have gone through the patch method. 所以我经历了补丁方法。 I am not getting how to use the patch method in google app engine endpoint to update only the price. 我没有得到如何在Google App Engine端点中使用补丁方法来仅更新价格。

This is my Bike model 这是我的自行车模特

@Entity

public class Bike { 公共课自行车{

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

this is my insert api 这是我的插入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();
}

in the similar way i want to use patch method to update only the price of Bike. 以类似的方式,我想使用补丁方法仅更新Bike的价格。

I don't think you can use the PATCH HTTP method with Google Cloud Endpoints, see the doc for @ApiMethod Annotation which says "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 ). 我认为您不能将PATCH HTTP方法与Google Cloud Endpoints一起使用,请参阅@ApiMethod Annotation的文档,其中说:“将实体作为参数的方法应该使用HttpMethod.POST(用于插入操作)或HttpMethod.PUT(以进行更新操作)”( https://cloud.google.com/endpoints/docs/frameworks/java/annotations )。

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

What you can do if you want to avoid sending a complete "Bike" resource representation (for eg use less bandwidth) is to create a specific class (annotated as an @Entity) which only has the two necessary fields. 如果要避免发送完整的“自行车”资源表示(例如,使用较少的带宽),该怎么做是创建一个只有两个必填字段的特定类(标注为@Entity)。 Let's call it BikePrice for example 例如,我们称它为BikePrice

@Entity
public class BikePrice {

    @Id
    protected Long id;

    protected String price;

You then create a dedicated endpoints method (with a BikePrice entity as parameter) in which you load your Bike original entity through Objectify and update it 然后,您创建一个专用的终结点方法(以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());
....

Note that you never save any BikePrice entity in the Datastore. 请注意,您永远不会在数据存储区中保存任何BikePrice实体。 It is just used as a kind of "container" or "conveyor of data" between your front end and App Engine. 它只是用作前端和App Engine之间的一种“容器”或“数据传送器”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM