我是Restlet的新手,我遇到了问题。我创建了一个完美的服务但我的客户端应用程序没有。我调用DELETE方法时失败。 GET,POST和PUT工作正常。
这是我的代码(客户端):
JSONObject object = new JSONObject();
object.put("name", param);
Request request = new Request(Method.DELETE, new Reference("http://localhost:8181/Restlet_Service/"));
request.setEntity(new JsonRepresentation(object.toString()));
Client cliente = new Client(Protocol.HTTP);
Response response = client.handle(request);
String text = response.getEntity().getText();
服务代码。 GET,POST和PUT方法以相同的代码开头:
public class MyResource extends ServerResource {
@Delete
public Representation deleteUser(Representation entity) {
try {
String jsonString = (new JsonRepresentation(entity)).getJsonObject().toString();
Map map = (Map) (new JSONParser()).parse(jsonString);
if(map.size() > 0) {
String message = dbc.deleteUser(map.get("name").toString());
return new StringRepresentation(message, MediaType.TEXT_PLAIN);
} else {
return new StringRepresentation("Unable to delete user", MediaType.TEXT_PLAIN);
}
} catch (Exception ex) {
return new StringRepresentation("ERROR: " + ex.getMessage(), MediaType.TEXT_PLAIN);
}
}
... }
我在Chrome中使用Advanced Rest Client测试了每种方法,并且所有方法都运行良好。它仅在我的客户端应用程序中失败。这是例外:
java.lang.NullPointerException
at java.io.StringReader.<init>(Unknown Source)
at org.json.JSONTokener.<init>(JSONTokener.java:66)
at org.json.JSONObject.<init>(JSONObject.java:402)
at org.restlet.ext.json.JsonRepresentation.getJsonObject(JsonRepresentation.java:199)
at service.MyResource.borrarUsuario(MyResource.java:95)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.restlet.resource.ServerResource.doHandle(ServerResource.java:503)
at org.restlet.resource.ServerResource.delete(ServerResource.java:208)
at org.restlet.resource.ServerResource.doHandle(ServerResource.java:594)
at org.restlet.resource.ServerResource.doNegotiatedHandle(ServerResource.java:649)
at org.restlet.resource.ServerResource.doConditionalHandle(ServerResource.java:348)
at org.restlet.resource.ServerResource.handle(ServerResource.java:952)
at org.restlet.resource.Finder.handle(Finder.java:246)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Router.doHandle(Router.java:431)
at org.restlet.routing.Router.handle(Router.java:648)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.engine.application.StatusFilter.doHandle(StatusFilter.java:155)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.engine.CompositeHelper.handle(CompositeHelper.java:211)
at org.restlet.engine.application.ApplicationHelper.handle(ApplicationHelper.java:84)
at org.restlet.Application.handle(Application.java:381)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Router.doHandle(Router.java:431)
at org.restlet.routing.Router.handle(Router.java:648)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Router.doHandle(Router.java:431)
at org.restlet.routing.Router.handle(Router.java:648)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.engine.CompositeHelper.handle(CompositeHelper.java:211)
at org.restlet.Component.handle(Component.java:392)
at org.restlet.Server.handle(Server.java:516)
at org.restlet.engine.ServerHelper.handle(ServerHelper.java:72)
at org.restlet.engine.adapter.HttpServerHelper.handle(HttpServerHelper.java:152)
at org.restlet.ext.servlet.ServerServlet.service(ServerServlet.java:1089)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
may 22, 2013 3:10:58 PM org.restlet.engine.log.LogFilter afterHandle
INFO: 2013-05-22 15:10:58 127.0.0.1 - 127.0.0.1 8181 DELETE /Restlet_Service/ - 200 11 17 20592 http://localhost:8181 Restlet-Framework/2.1.2 -
这是因为请求为空而我不知道为什么。 欢迎任何帮助。谢谢!
答案 0 :(得分:0)
我的猜测是你的客户端没有发送任何表示,所以实体是NULL,这反过来导致Json Tokenizer中的NPE。