ArrayIndexOutOfBoundsException:Web服务器中的1

时间:2015-05-14 08:40:22

标签: java exception-handling

我使用tomcat web容器和jersey库创建了一个简单的RESTful Web服务。

我只覆盖了post方法,其余都使用了默认设置。

从服务器计算机POST发送curl查询时,我得到了预期的响应。我也从其他计算机获得GET查询的预期响应,但对POST我得到java.lang. ArrayIndexOutOfBoundsException: 1异常错误。 post方法如下:

@POST
@Produces(MediaType.TEXT_PLAIN)
public String post(String str) {
  String[] parts = str.split("&");
  String[] param1 = parts[0].split("=");//value1=param1[1] (one/two): query type
  String[] param2 = parts[1].split("=");//value2=param2[1] 
  String[] param3 = parts[2].split("=");//value3=param3[1] 
  String[] param4 = parts[3].split("=");//value4=param4[1] 

  if(param1[1].equals("one")){
      return hashGenerator(param2[1],param3[1]);
  }else if(param1[1].equals("two")){
      return saveInput(param2[1],param3[1],param4[1]);
  }
  return "Unexpected Query";
}

来自服务器的响应是:

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: java.lang.ArrayIndexOutOfBoundsException: 1
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:421)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:222)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

root cause

java.lang.ArrayIndexOutOfBoundsException: 1
com.va.jersey.helloworld.Hello.post(Hello.java:39)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:164)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:181)
org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:203)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:101)
org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:305)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
org.glassfish.jersey.internal.Errors.process(Errors.java:315)
org.glassfish.jersey.internal.Errors.process(Errors.java:297)
org.glassfish.jersey.internal.Errors.process(Errors.java:267)
  org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:288)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1110)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:401)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:222)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
你能否提出可能的错误......

1 个答案:

答案 0 :(得分:1)

您要求的数组元素可能不存在,因此当您尝试使用这些不存在的数组元素时,将该输入字符串拆分到字符上并且不检查数组的长度总是有可能抛出ArrayIndexOutOfBoundsException稍后(输入字符串foo&bar会这样做。)

此外,你永远不应该自己解析表单数据/查询字符串,边缘情况太多了。使用库,或者更好地使用你的框架(泽西岛) - 它作为JAX-RS的一部分,有support for Form Params。请参阅an example here

或者你可以使它更加RESTful并使用PathParam来明确地在请求中获取参数,如果这是一个选项。