Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

what is the cause of java.lang.StringIndexOutOfBoundsException: String index out of range: 1 in this simplified example of Rest Services?

curl -X GET http://localhost:8080/julian/resources/MyRestService/convert/222

GlassFish Server Open Source Edition 5.1.0 - Error report

HTTP Status 500 - Internal Server Error


type Exception report

messageInternal Server Error

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

exception

javax.servlet.ServletException: java.lang.StringIndexOutOfBoundsException: String index out of range: 1

root cause

java.lang.StringIndexOutOfBoundsException: String index out of range: 1

note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 5.1.0 logs.


GlassFish Server Open Source Edition 5.1.0

(base)
package book.jakarta.julian;

import javax.websocket.server.PathParam;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Application;
/**
 * 
 * Rest Web Service
 *
 */
@ApplicationPath("/resources")
@Path("MyRestService")
public class Julian  extends Application {
        
    // http://localhost:8080/julian/resources/MyRestService/convert/0123
    @GET
    @Produces("text/plain") 
    @Path("convert/{inNum : .*}")
            
    public String add(
            @PathParam("inNum") String inNum) {
        
          // get first digit ignore rest 
          int intemp = Integer.parseInt(inNum.substring(0, 1));
            
          return "" + (intemp + 3) ;
      }
}
question from:https://stackoverflow.com/questions/65890112/what-is-the-cause-of-java-lang-stringindexoutofboundsexception-string-index-out

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...