java - ArrayIndexOutOfBoundsException: 1 in web server -


i have made simple restful web service using tomcat web container , jersey library.

i have overridden post method, rest default settings used.

while sending post query curl server machine expected response. other machine also, getting expected response get query, post java.lang. arrayindexoutofboundsexception: 1 exception error. post method goes this:

@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"; } 

response server :

description server encountered internal error prevented fulfilling 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) 

can please suggest fault....

you asking array elements might not exist, splitting input string on character , not checking length of array risks throwing arrayindexoutofboundsexception when try use these non-existent array elements later (an input string of foo&bar it).

furthermore, should never form data / query string parsing yourself, there many edge cases. use library, or better still use framework (jersey) - it, part of jax-rs, has support form params. see an example here.

or make more restful , use pathparams take parameters explicitly in request, if that's option.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -