当我想在请求 validator 中检索帖子的 http 正文时,它会重置我的实体,并且当我想在资源类中获取 http 正文时,我会收到空指针异常。
验证者:
JsonRepresentation jsonrep;
try {
Representation entity = request.getEntity();
jsonrep = new JsonRepresentation(entity);
//bug: entity resets when getJsonObject is being called.
JSONObject jsonobj = jsonrep.getJsonObject();
if(companyId != jsonobj.getInt("id_companies")){
return Verifier.RESULT_INVALID;
}
...
应用程序资源:
@Post
public Representation addApp(Representation rep) throws Exception{
//rep is null
JsonRepresentation jsonrep = new JsonRepresentation(rep);
当我不打电话时:
JSONObject jsonobj = jsonrep.getJsonObject();
它工作得很好。
有人遇到同样的问题或有解决方案吗?
提前致谢!
最佳答案
事实上,默认情况下,表示是一个不存储表示内容的InputRepresentation。
就您而言,最简单的方法是将表示形式包装到验证程序中的 StringRepresentation 中:
Representation entity = request.getEntity();
StringRepresentation sEntity = new StringRepresentation(entity);
request.setEntity(sEntity);
JsonRepresentation jsonrep = new JsonRepresentation(sEntity);
然后,字符串表示形式将自动提供给您的服务器资源方法...
希望对您有帮助。 蒂埃里
关于java - ReSTLet:在 validator 中检索 http 正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15252285/