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

穿精又带淫゛_ 提交于 2020-12-08 06:39: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?


回答1:


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?




回答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



来源:https://stackoverflow.com/questions/37703383/java-net-malformedurlexception-unknown-protocol-localhost-at-controller-restco

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!