java.net.MalformedURLException:未知协议:controller.RestController.addService(RestController.java:62)上的localhost [英] java.net.MalformedURLException: unknown protocol: localhost at controller.RestController.addService(RestController.java:62)

查看:111
本文介绍了java.net.MalformedURLException:未知协议:controller.RestController.addService(RestController.java:62)上的localhost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

控制器代码

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();
    }
 }

这是我的jsp视图

<form:form method="POST" commandName="servDetForm" action="AddService">
              <table style="appearance:dialog ">

                    <tr>
                        <td>Number</td>
                        <td><form:input path="Numbers"/></td>
                    </tr>

我哪里错了?

推荐答案

URL应为:

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

也许

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

(更新-我假设您的URL中具有正确的路径.如果没有,那么您也需要修复该路径.)

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

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

(URI解析器将第一个冒号之前的内容解释为协议.在断开的URL中,这意味着主机名("localhost")被错误地视为协议字符串.但是,没有注册的协议具有该名称的协议的处理程序...因此解析器说的是未知协议".)

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

和这个:

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

"/"字符的含义是什么?

What is the significance of the "/" character?

这篇关于java.net.MalformedURLException:未知协议:controller.RestController.addService(RestController.java:62)上的localhost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆