我有一个Java Google Endpoint,如下所示
@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
(start,end,partyName)中的变量为null
。在从1.0迁移到2.0之前,它完美运行。发生了什么事?
答案 0 :(得分:0)
我相信您可能想尝试将Content-Type
标题设置为application/json
。