java.net.MalformedURLException: 未知协议: localhost at controller.RestController.addService(RestController.java:62)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37703383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:41:37  来源:igfitidea点击:

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

javaspringjsphttpspring-mvc

提问by Blaze

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

我正在尝试向服务器发送 http 帖子,但我的控制器收到格式错误的 url 异常

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

这是我的jsp视图

<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?

我哪里错了?

回答by Stephen C

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.)

更新- 我假设你的 URL 中有正确的路径。如果没有,那么你也需要修复它。)

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.)

“http”或“https”是解析器正在寻找的 URL 的协议部分。没有协议的 URL 不是有效的 URL。 (它是一个相对 URI,只能相对于另一个 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".)

(URI 解析器将第一个冒号之前的内容解释为协议。在您损坏的 URL 中,这意味着主机名(“localhost”)被错误地视为协议字符串。但是,没有注册的协议处理程序用于具有该名称的协议......所以解析器说的是“未知协议”。)



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?

“/”字符的意义是什么?

回答by Munny

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

格式错误的 url 异常与您传递的 URI 相关 "REST_SERVICE_URI"

Need to pass a REST_SERVICE_URIwith the valid protocol like http, httpsetc

需要通过REST_SERVICE_URI有效的协议,如http, https