java.net.MalformedURLException: unknown protocol: localhost at controller.RestController.addService(RestController.java:62)

19,863

The URL should be this:

"http://localhost:8081/ItaxServ/create"

or maybe

"https://localhost:8081/ItaxServ/create"

(UPDATE - I'm assuming that you have the correct path in your URL. If not, then you will need to fix that too.)

The "http" or "https" is the protocol part of the URL that the parser is looking for. A URL without a protocol is not a valid URL. (It is a relative URI, and can only be resolved with respect to another URL.)

(The URI parser is interpreting the stuff before the first colon as the protocol. In your broken URL, that means that the hostname ("localhost") is being incorrectly treated as a protocol string. However, there is no registered protocol handler for a protocol with that name ... so the parser is saying "unknown protocol".)


Hint: Can you see the difference between this:

@RequestMapping(value = "AddService", method = RequestMethod.POST)

and this:

@RequestMapping(value = "/create", method = RequestMethod.POST)

What is the significance of the "/" character?

Share:
19,863
Blaze
Author by

Blaze

Updated on July 25, 2022

Comments

  • Blaze
    Blaze almost 2 years

    I am trying to make a http post to server and I am getting a malformed url exception from my controller

    controller code

    public static final String REST_SERVICE_URI = "localhost:8081/create";
    

    the method in the controller that receives the request from the server

    @RequestMapping(value="AddService",method = RequestMethod.POST)
    @ResponseBody
     public void addService(@ModelAttribute("servDetForm")) throws IOException{
        //return dataServices.addService(tb);
    
         URL serv;
         URLConnection yc;
        try {
            serv = new URL(REST_SERVICE_URI);
              yc = serv.openConnection();
            try {
                yc = serv.openConnection();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             BufferedReader in;
             in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
    
             String inputLine;
    
             while ((inputLine = in.readLine()) != null) 
                 System.out.println(inputLine);
             in.close();
    
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     }
    

    this is my jsp view

    <form:form method="POST" commandName="servDetForm" action="AddService">
                  <table style="appearance:dialog ">
    
                        <tr>
                            <td>Number</td>
                            <td><form:input path="Numbers"/></td>
                        </tr>
    

    where is my wrong?

  • Bünyamin Şentürk
    Bünyamin Şentürk over 2 years
    this should be marked as answer. adding http:// solved my issue
  • user207421
    user207421 about 2 years
    OP is not 'passing [it] as "REST_SERVICE_URI"'.