![](/img/trans.png)
[英]unable to call rest web service from html/javascript with parameter
[英]Unable to call rest web service from html form action attribute
我試圖從HTML形式調用用Java編寫的其余Web服務
我的網絡服務代碼是
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> ";
}
}
我的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>
在html表單中,我將Web服務稱為../rest/hello。 我從http://www.vogella.com/articles/REST/引用了示例
有人可以告訴我該怎么做嗎?
謝謝
我在您的代碼中看到了多個問題。 第一個是您放置REST服務路徑映射的方式:
@Path("/hello{name}")
我不認為您可以串聯兩個路徑參數,我認為這是一個錯字,並且希望此映射為:
@Path("/hello/{name}")
第二個問題是您的html代碼。 您正在嘗試將名稱作為FORM
參數發送,這對於POST
請求而不是GET請求很有用。 GET
請求期望URL或路徑中的參數與您期望的一樣,它們包含在REST服務代碼中。
現在,您有兩個選擇可以更正代碼。 將REST服務代碼方法從GET
更改為POST
。 或者,您可以從HTML發送名稱作為路徑參數,以正確訪問服務並獲取參數。
如果將方法更改為POST,則可能必須將參數更改為FormParam
而不是PathParam
。
在您的代碼中,將文本框的名稱更改為name。 還要更改方法“ @Path(” / hello {name}“)”上方的路徑。
嘗試使用此路徑和html中的url。“ @ Path(” / test“)”
網址:/ helpdesk / rest / hello / test
你好-是根類查找器
test-是根類中的方法查找器
請參閱鏈接“ http://www.mastertheboss.com/resteasy/resteasy-tutorial-part-two-web-parameters ”,以獲取路徑參數和形式參數之間的差異
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.