0

我正在使用objectify +應用程序引擎端點以將數據保存到雲中。 我已創建我的 「點」 實體(這是在谷歌地圖上的點)與客觀化:用appengine,400壞請求物化。沒有課程註冊

package com.example.Javier.WH.backend; 

import com.google.appengine.repackaged.com.google.type.LatLng; 
import com.googlecode.objectify.annotation.Entity; 
import com.googlecode.objectify.annotation.Id; 

@Entity 
public class Point { 

@Id 
String id; 
Double latitude; 
Double longitude; 
Double ratius; 
String date; 
String title; 
String description; 

public Point(){} 

public Point(Double la, Double lo,String stitle){ 
    latitude = la; 
    longitude = lo; 
    title = stitle; 
} 

public String getId(){ 
    return id; 
} 

public void setId(String id){ 
    this.id = id; 
} 

public Double getLatitude() { 
    return latitude; 
} 

public void setLatitude(Double latitude) { 
    this.latitude = latitude; 
} 

public Double getLongitude() { 
    return longitude; 
} 

public void setLongitude(Double longitude) { 
    this.longitude = longitude; 
} 

public Double getRatius() { 
    return ratius; 
} 

public void setRatius(Double ratius) { 
    this.ratius = ratius; 
} 

public String getDate() { 
    return date; 
} 

public void setDate(String date) { 
    this.date = date; 
} 

public String getTitle() { 
    return title; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 

public String getDescription() { 
    return description; 
} 

public void setDescription(String description) { 
    this.description = description; 
} 
} 

,這是我的 「pointEndpoint」 類:

package com.example.Javier.WH.backend; 

import com.google.api.server.spi.config.Api; 
import com.google.api.server.spi.config.ApiMethod; 
import com.google.api.server.spi.config.ApiNamespace; 
import com.google.api.server.spi.config.Named; 
import com.google.api.server.spi.config.Nullable; 
import com.google.api.server.spi.response.CollectionResponse; 
import com.google.api.server.spi.response.ConflictException; 
import com.google.api.server.spi.response.NotFoundException; 
import com.google.appengine.api.datastore.Cursor; 
import com.google.appengine.api.datastore.QueryResultIterator; 
import com.googlecode.objectify.cmd.Query; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.logging.Logger; 

import static com.googlecode.objectify.ObjectifyService.ofy; 

@Api(
     name = "pointEndpoint", 
     version = "v1", 
     namespace = @ApiNamespace(
       ownerDomain = "backend.WH.Javier.example.com", 
       ownerName = "backend.WH.Javier.example.com", 
       packagePath = "") 
    ) 
public class PointEndpoint { 

    private static final Logger LOG = Logger.getLogger(PointEndpoint.class.getName()); 

    public PointEndpoint(){} 

    @ApiMethod(name = "listPoints") 
    public CollectionResponse<Point> listPoints(@Nullable @Named("cursor") String cursorString){ 

     Query<Point> query = ofy().load().type(Point.class); 
     if (cursorString != null && cursorString != ""){ 
      query = query.startAt(Cursor.fromWebSafeString(cursorString)); 
     } 
     List<Point> records = new ArrayList<Point>(); 
     QueryResultIterator<Point> iterator = query.iterator(); 
     int num = 0; 
     while (iterator.hasNext()){ 
      records.add(iterator.next()); 
     } 
     if (cursorString != null && cursorString != "") { 
      Cursor cursor = iterator.getCursor(); 
      if (cursor != null) { 
       cursorString = cursor.toWebSafeString(); 
      } 
     } 
     return CollectionResponse.<Point>builder().setItems(records).setNextPageToken(cursorString).build(); 
    } 

    @ApiMethod(name = "insertPoint") 
    public Point insertPoint(Point point) throws ConflictException{ 
     if(point.getId() != null){ 
      if(findRecord(point.getId()) != null){ 
       throw new ConflictException("Object already exists"); 
      } 
     } 
     ofy().save().entity(point).now(); 
     return point; 
    } 

    @ApiMethod(name = "insertString") 
    public Point insertString(@Named("string") String s) throws ConflictException{ 
     Point point = new Point(49.500087,7.829500,s); 
     if(point.getId() != null) { 
      if (findRecord(point.getId()) != null) { 
       throw new ConflictException("Object already exists"); 
      } 
     } 
     ofy().save().entity(point).now(); 
     return point; 
    } 

    @ApiMethod(name = "updatePoint") 
    public Point updatePoint(Point point) throws NotFoundException{ 
     if(findRecord(point.getId()) == null){ 
      throw new NotFoundException(("Point record does not exist")); 
     } 
     ofy().save().entity(point).now(); 
     return point; 
    } 

    @ApiMethod(name = "removePoint") 
    public void removeQuote(@Named("id") String id) throws NotFoundException{ 
     Point record = findRecord(id); 
     if(record == null){ 
      throw new NotFoundException("Point record does not exist"); 
     } 
     ofy().delete().entity(record).now(); 
    } 

    private Point findRecord(String id){ 
     return ofy().load().type(Point.class).id(id).now(); 
    } 

} 

的 「ofyService」 類:

package com.example.Javier.WH.backend; 

import com.googlecode.objectify.Objectify; 
import com.googlecode.objectify.ObjectifyFactory; 
import com.googlecode.objectify.ObjectifyService; 

public class OfyService { 

    static { 
     ObjectifyService.register(Point.class); 
     ObjectifyService.register(MyEndpoint.class); 
    } 

    public static Objectify ofy(){ 
     return ObjectifyService.ofy(); 
    } 

    public static ObjectifyFactory factory(){ 
     return ObjectifyService.factory(); 
    } 
} 

的build.gradle:

def appEmail = "[email protected]" 

buildscript { 
    repositories { 
     jcenter() 
    } 
    dependencies { 
     classpath 'com.google.appengine:gradle-appengine-plugin:1.9.34' 
    } 
} 

repositories { 
    jcenter(); 
} 

apply plugin: 'java' 
apply plugin: 'war' 
apply plugin: 'appengine' 

sourceCompatibility = JavaVersion.VERSION_1_7 
targetCompatibility = JavaVersion.VERSION_1_7 

dependencies { 
    appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.34' 
    compile 'com.google.appengine:appengine-endpoints:1.9.34' 
    compile 'com.google.appengine:appengine-endpoints-deps:1.9.34' 
    compile 'com.googlecode.objectify:objectify:5.0.3' 
    compile 'javax.servlet:servlet-api:2.5' 
} 

appengine { 
    downloadSdk = true 
    appcfg { 
     oauth2 = true 
      email = "${appEmail}" 
    } 
    endpoints { 
     getClientLibsOnBuild = true 
     getDiscoveryDocsOnBuild = true 
    } 
} 

終於在web.xml:

<?xml version="1.0" encoding="utf-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"> 
    <servlet> 
     <servlet-name>SystemServiceServlet</servlet-name> 
     <servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class> 
     <init-param> 
      <param-name>services</param-name> 
      <param-value>com.example.Javier.WH.backend.MyEndpoint, com.example.Javier.WH.backend.PointEndpoint</param-value> 
     </init-param> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>SystemServiceServlet</servlet-name> 
     <url-pattern>/_ah/spi/*</url-pattern> 
    </servlet-mapping> 

    <welcome-file-list> 
     <welcome-file>index.html</welcome-file> 
    </welcome-file-list> 
</web-app> 

不知從哪裏錯誤來認識。請求似乎罰款

enter image description here

但錯誤總是出現

enter image description here

+1

刪除'進口靜態com.googlecode.objectify.ObjectifyService.ofy;'和進出口使用自己的'OfyService'註冊該課程。您的代碼目前完全沒有使用。 – zapl

回答

0

在你OfyService類,你有

​​3210

MyEndpoint.class聽起來就像標註有@Api和不與Objectify的@Entity。因此,我假設你在應用程序啓動期間或執行了OfyService的靜態構造函數時會得到一個異常。這種例外情況會阻止任何實體進行註冊,因此您的實際實體也未在Objectify中註冊。 這裏的簡單解決方案是不要嘗試使用Objectify註冊​​@Api。只註冊@Entity - 註釋類。

0

一下添加到web.xml文件:

<filter> 
    <filter-name>ObjectifyFilter</filter-name> 
    <filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>ObjectifyFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 
<listener> 
    <listener-class>com.example.Javier.WH.backend.OfyService </listener-class> 
</listener>