java 无法从 html 表单操作属性调用休息 Web 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17801623/
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-01 15:02:37  来源:igfitidea点击:

Unable to call rest web service from html form action attribute

javahtmlformsrest

提问by User007

I am trying to call rest web service written in java from html form

我正在尝试从 html 表单调用用 java 编写的 rest 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. I have referred example from http://www.vogella.com/articles/REST/

在 html 表单中,我调用了 Web 服务 ../rest/hello。我参考了http://www.vogella.com/articles/REST/ 中的示例

Can anybody please tell me how to do it?

有人可以告诉我该怎么做吗?

Thank you

谢谢

回答by Juned Ahsan

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. You are trying to send the name as a FORMparam, which is good for POSTrequests and not for GET requests. GETrequest expects the params in the URL or path as you are expecting it to be in your REST service code.

第二个问题是你的 html 代码。您正在尝试将名称作为FORM参数发送,这对POST请求而不是 GET 请求有好处。GETrequest 期望 URL 或路径中的参数,正如您期望它在您的 REST 服务代码中一样。

Now you have two choices to correct the code. Either change your REST service code method to POSTfrom GET. Or you can send the name as path param from your HTML to hit your service correctly and getting the parameter.

现在您有两种选择来更正代码。将您的 REST 服务代码方法更改为POSTfrom GET。或者,您可以将名称作为 HTML 中的路径参数发送,以正确访问您的服务并获取参数。

If you change the method to POST, you may have to change the parameter to FormParaminstead of PathParam.

如果将方法更改为 POST,则可能需要将参数更改为FormParam而不是PathParam

回答by Veera

In your code change the name of the textbox to name. Also change the path above the method "@Path("/hello{name}")".

在您的代码中,将文本框的名称更改为 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”以了解路径参数和表单参数之间的区别