2017-08-24 69 views
0

後,我有一個Java谷歌端點如下請求體空(?)遷移到端點Framework 2.0的

@ApiMethod(
     name="createShiftlist", 
     path="jeugdhuis/{websafeJeugdhuisKey}/shiftlists", 
     httpMethod="POST", 
     authenticators={FirebaseAuthenticator.class} 
     ) 
public void createShiftlist(User user, @Named("websafeJeugdhuisKey") String websafeJeugdhuisKey, ShiftlistForm shiftlistForm) 
     throws UnauthorizedException { 

    if (shiftlistForm.getStart() == null) 
     throw new IllegalArgumentException(shiftlistForm.getStart() + " " + shiftlistForm.getPartyName() + " " + shiftlistForm.getEnd()); 

    if (user == null) 
     throw new UnauthorizedException("User is not logged in!"); 

    if (!JukteUserAPI.isJeugdhuisAdmin(user, websafeJeugdhuisKey)) 
     throw new UnauthorizedException("Logged in user is not an admin of the given Jeugdhuis."); 

    OfyService.ofy().save().entities(new Shiftlist[] { 
     new Shiftlist(
     OfyService.factory().allocateId(Key.create(websafeJeugdhuisKey), Shiftlist.class).getId(), 
     websafeJeugdhuisKey, shiftlistForm.getPartyName(), shiftlistForm.getStart(), shiftlistForm.getEnd()) 
     }); 
} 

我用下面的.js文件調用它。

var jukteapi = jukteapi || {}; 
var jukteKey = 'myKey'; 

function XHRBuilder(appId, apiName, version) { 
    this.root = "https://" + appId + ".appspot.com/_ah/api/" + apiName + "/" + version + "/"; 
    this.params; 
    this.method; 
    this.endpoint; 
    this.authorizationToken; 
    this.onsucces; 

    this.get = function() { 
     this.method = 'GET'; 
     return this; 
    }; 

    this.post = function() { 
     this.method = 'POST'; 
     return this; 
    }; 

    this.delete = function() { 
     this.method = 'DELETE'; 
     return this; 
    }; 

    this.put = function() { 
     this.method = 'PUT'; 
     return this; 
    } 

    this.path = function(endpointPath) { 
     this.endpoint = endpointPath; 
     return this; 
    }; 

    this.authorizationToken = function(token) { 
     this.authorizationToken = token; 
     return this; 
    }; 

    this.onsucces = function(func) { 
     this.onsucces = func; 
     return this; 
    }; 

    this.addParam = function(paramName, paramValue) { 
     if (this.params === undefined) 
      this.params = new FormData(); 
     this.params.append(paramName, paramValue); 
     return this; 
    }; 

    this.send = function() { 
     var xhr = new XMLHttpRequest(); 
     xhr.open(this.method, this.root + this.endpoint); 

     var self = this.onsucces; 
     xhr.onreadystatechange = function() { 
      if (xhr.readyState == 4 && xhr.status == 200) 
       self(JSON.parse(xhr.responseText)); 
      else if (xhr.readyState == 4 && xhr.status == 204) 
       self(); 
      else if (xhr.readyState == 4 && xhr.status == 400) 
       alert(JSON.parse(xhr.responseText).error.message); 
     }; 

     xhr.setRequestHeader('Authorization', 'Bearer ' + this.authorizationToken); 

     if (typeof this.params !== "undefined") 
      for (var pair of this.params.entries()) { 
       console.log(pair[0]+ ', ' + pair[1]); 
      } 
     xhr.send(this.params); 
    }; 
} 

jukteapi.http = function() { 
    return new XHRBuilder('jhjukte','jukte','v1') 
} 

jukteapi.createShiftlist = function(onsucces, name, start, end) { 
    firebase.auth().currentUser.getIdToken().then(function(idToken) { 
     jukteapi.http().post() 
     .path('jeugdhuis/' + jukteKey + '/shiftlists') 
     .authorizationToken(idToken) 
     .addParam('partyName', name) 
     .addParam('start', start + ':00') 
     .addParam('end', end + ':00') 
     .onsucces(onsucces) 
     .send(); 
    }); 
} 

這是ShiftlistForm類。

package jukte.form; 

import java.util.Date; 

public class ShiftlistForm { 
    private String partyName; 
    private Date start; 
    private Date end; 

    @SuppressWarnings("unused") 
    private ShiftlistForm() {} 

    public ShiftlistForm(String partyName, Date start, Date end){ 
     this.partyName = partyName; 
     this.start = start; 
     this.end = end; 
    } 

    public String getPartyName() { 
     return partyName; 
    } 

    public Date getStart() { 
     return start; 
    } 

    public Date getEnd() { 
     return end; 
    } 

    public void setPartyName(String partyName) { 
     this.partyName = partyName; 
    } 

    public void setStart(Date start) { 
     this.start = start; 
    } 

    public void setEnd(Date end) { 
     this.end = end; 
    } 
} 

端點被調用,但在ShiftlistForm變量(開始,結束,partyName)是null。它在我從1.0遷移到2.0之前完美運行。到底是怎麼回事?

回答

0

我相信你可能想嘗試將你的Content-Type標題設置爲application/json