11

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?

2
  • 1
    localhost is host name and not a protocol. You need to specify a protocol http/https etc in REST_SERVICE_URI. So it should be like http://localhost:8081/ItaxServ/create Commented Jun 8, 2016 at 13:16
  • using this localhost:8081/ItaxServ/create gives error like java.io.IOException: Server returned HTTP response code: 400 for URL: localhost:8081/ItaxServ/create Commented Jun 8, 2016 at 13:25

2 Answers 2

15

The URL should be this:

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

or maybe

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

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 (in your case "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: localhost".

Sign up to request clarification or add additional context in comments.

1 Comment

this should be marked as answer. adding http:// solved my issue
-2

malformed url exception is something related to URI which you passing as "REST_SERVICE_URI"

Need to pass a REST_SERVICE_URI with the valid protocol like http, https etc

1 Comment

OP is not 'passing [it] as "REST_SERVICE_URI"'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.