简体   繁体   English

无法通过html表单操作属性调用其余Web服务

[英]Unable to call rest web service from html form action attribute

I am trying to call rest web service written in java from html form 我试图从HTML形式调用用Java编写的其余Web服务

My web service code is 我的网络服务代码是

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class Hello {

      @POST
      @Path("/hello")
      @Consumes(MediaType.TEXT_HTML)
      @Produces(MediaType.TEXT_HTML)
      public String hello( @FormParam("username") String name1) {
  return "<html> " + "<title>" + "Hello Jersey" + "</title>"
            + "<body><h1>" + "Hello from helpdesk" + "</body></h1>" + "</html> ";
      }

}

And my html page is 我的html页面是

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form action= "http://localhost:8080/helpdesk/rest/hello" method="POST">
        Username: <input type="text" name="username">
        <p>
        <input type="submit" value="Submit">
    </form>

</body>
</html>

Here in the html form, i have called the web service ../rest/hello. 在html表单中,我将Web服务称为../rest/hello。 I have referred example from http://www.vogella.com/articles/REST/ 我从http://www.vogella.com/articles/REST/引用了示例

Can anybody please tell me how to do it? 有人可以告诉我该怎么做吗?

Thank you 谢谢

I see multiple problems in your code. 我在您的代码中看到了多个问题。 First one is the way you have put the mapping of your REST service path: 第一个是您放置REST服务路径映射的方式:

  @Path("/hello{name}")

I don't think you can concatenate your two path params, I assume it to be a typo and expect this mapping to be: 我不认为您可以串联两个路径参数,我认为这是一个错字,并且希望此映射为:

  @Path("/hello/{name}")

Second problem is with your html code. 第二个问题是您的html代码。 You are trying to send the name as a FORM param, which is good for POST requests and not for GET requests. 您正在尝试将名称作为FORM参数发送,这对于POST请求而不是GET请求很有用。 GET request expects the params in the URL or path as you are expecting it to be in your REST service code. GET请求期望URL或路径中的参数与您期望的一样,它们包含在REST服务代码中。

Now you have two choices to correct the code. 现在,您有两个选择可以更正代码。 Either change your REST service code method to POST from GET . 将REST服务代码方法从GET更改为POST Or you can send the name as path param from your HTML to hit your service correctly and getting the parameter. 或者,您可以从HTML发送名称作为路径参数,以正确访问服务并获取参数。

If you change the method to POST, you may have to change the parameter to FormParam instead of PathParam . 如果将方法更改为POST,则可能必须将参数更改为FormParam而不是PathParam

In your code change the name of the textbox to name. 在您的代码中,将文本框的名称更改为name。 Also change the path above the method "@Path("/hello{name}")". 还要更改方法“ @Path(” / hello {name}“)”上方的路径。

Try with this path and url in html."@Path("/test")" 尝试使用此路径和html中的url。“ @ Path(” / test“)”

URL : /helpdesk/rest/hello/test 网址:/ helpdesk / rest / hello / test

hello - is root class finder 你好-是根类查找器

test - is method finder in the root class test-是根类中的方法查找器

Refer the link " http://www.mastertheboss.com/resteasy/resteasy-tutorial-part-two-web-parameters " for difference between path param and form param 请参阅链接“ http://www.mastertheboss.com/resteasy/resteasy-tutorial-part-two-web-parameters ”,以获取路径参数和形式参数之间的差异

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM