使用Restlet Java框架发布
我一点问题都没有。 使用POST请求尝试时,我收到以下消息: 内部服务器错误 服务器遇到意外情况,无法满足请求 我正在用Chrome的简单REST客户端扩展对其进行测试,但在实际应用中我得到了相同的信息 这是我的帖子:使用Restlet Java框架发布,java,rest,restlet,restlet-2.0,Java,Rest,Restlet,Restlet 2.0,我一点问题都没有。 使用POST请求尝试时,我收到以下消息: 内部服务器错误 服务器遇到意外情况,无法满足请求 我正在用Chrome的简单REST客户端扩展对其进行测试,但在实际应用中我得到了相同的信息 这是我的帖子: @Post public StringRepresentation pepe(Representation entity) { StringRepresentation result = this.users(); // Parse th
@Post
public StringRepresentation pepe(Representation entity) {
StringRepresentation result = this.users();
// Parse the given representation and retrieve data
Form form = new Form(entity);
String action = form.getFirstValue("action");
if(action.equals("add")){
//nothing
}
Db.closeConnection();
return result;
}
这是我的“正常工作”:
@Get
public StringRepresentation pepe() {
String action = getQuery().getValues("action");
StringRepresentation result = null;
result = this.users();
Db.closeConnection();
return result;
}
有趣的是:每当我删除条件ifaction.equalsadd{,它在POST中是空的,就可以正常工作。
这将有助于:
@Post
public StringRepresentation pepe(Representation entity) {
StringRepresentation result = this.users();
// Parse the given representation and retrieve data
Form form = new Form(entity);
String action = form.getFirstValue("action");
Db.closeConnection();
return result;
}
发生了什么?看起来很随机!是的,如果表单负载中没有输入操作,则变量操作将为null
您可以注意到,Restlet提供了一个带有默认值参数的getFirstValue方法:
String action = form.getFirstValue("action", "defaultActionValue");
这可以帮助您避免出现NullPointerException
否则,您似乎试图为一篇方法文章实现几个操作。我认为这篇博客文章可以给您一些额外的提示:
希望它能帮助你,
Thierry检查您的操作变量是否不为null。您可能没有看到NullPointerException。是的,您是对的。