[英]Ajax requests in the RESTful client
我使用Java RESTful
服务和使用方客户端。 Java应用正在接收和传递JSON
格式的数据。 我需要讨论2个请求及其在使用中的RESTful client
。
如果单击按钮,则会生成WalletInfo
实体并将其存储在数据库中。 它具有id
, name
, address
和currency
等属性。 点击本质上是一个POST
请求。
提供了定义,
@POST
@Path("generateAddress")
@Consumes(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_JSON})
WalletInfo generateAddress(GenerateWallet generateWallet);
这是实现,
public synchronized WalletInfo generateAddress(GenerateWallet generateWallet) {
String walletName = generateWallet.getWalletName();
String currencyName = generateWallet.getCurrencyName();
WalletInfo walletInfo = iWalletInfoDao.getWalletInfoWithWalletNameAndCurrency(walletName, currencyName);
return walletInfo;
}
我使用此cURL
命令执行POST
请求,
curl -H "Content-Type: application/json" -X POST -d '{"walletName": "Rome12345","currencyName":"Bitcoin"}' http://localhost:8080/rest/wallet/generateAddress
显然,由于它是POST
请求,因此在terminal
中不返回任何内容。 我还有另一个GET
请求,提供了定义,
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("currencyandname/{walletName}/{currencyName}")
WalletInfo getWalletInfoWithCurrencyAndWalletName(@PathParam("walletName") String walletName,
@PathParam("currencyName") String currencyName);
这是实现,
public WalletInfo getWalletInfoWithCurrencyAndWalletName(String walletName, String currencyName) {
return iWalletInfoDao.getWalletInfoWithWalletNameAndCurrency(walletName, currencyName);
}
我对执行这样的cURL
请求,
curl -X GET http://localhost:8080/rest/wallet/currencyandname/test1/Bitcoin | json
如果名称和货币信息与数据库中的信息匹配,它将返回WalletInfo
实体的信息。 例如,我们使用cURL请求提供名称test1
和货币Bitcoin
,并在终端中获得类似内容,
{
"id": 1,
"name": "test1",
"address": "mp51mPC38Wtcmybdyd9MPEB2bKnw6eYbCs",
"currency": "Bitcoin"
}
如果没有匹配项,我们将一无所获。 当我们找到匹配项时,我们可以使用其他信息,主要是地址。
让我们谈谈客户。 如我之前所说,使用方客户端中有一个按钮来生成地址并用该地址填充下拉菜单。 点击按钮后,以下POST
请求将在客户端中执行,
var dataValue = {
"walletName": "walletName",
"currencyName": "selectedCurrency"
};
dataValue = JSON.stringify(dataValue);
$.ajax({
type: "POST",
url: "www.hostdomain.com" + "/rest/wallet/generateAddress",
data: dataValue,
contentType: "application/json",
dataType: "json",
success: function(data){
// the GET request code will be here
},
failure: function(errMsg) {
// alert(errMsg);
}
});
POST请求正确吗? 我的意思是,当我在终端中执行POST
CURL命令时,没有任何回报。 但是,成功执行POST
请求后, success function(data)
似乎需要data
作为输入参数。 在Java方法中,我还将WalletEntity
作为返回类型。
2nd Question
如果POST
请求成功,则意味着将生成WalletEntity并将其存储在MySQL中。 现在,我想执行GET
请求,以使用名称/货币名称获取相同的WalletInfo
实体,并使用该实例中的address参数。
它必须位于POST
请求的success: function(data)
内部,因为我们仅在成功创建钱包后才执行GET请求,并将为此目的使用Java应用程序的getWalletInfoWithCurrencyAndWalletName
方法。 我将GET
请求成像为某种东西,
$.ajax({
url: "www.hostdomain.com"+ "/rest/wallet/currencyandname/" + "wallet_Name" + "/" + "selected_Currency",
type: "GET",
dataType: "json",
success: function (data) {
// I should have the WalletEntity here as the data
// from the `GET` request
$("#address").append($('<option></option>').attr("value", "1").text(walletName + " " + data['address'].toString()));
}
});
我假设数据是WalletEntity
,地址是它的参数之一。 因此,如果我只希望有address
,那么我需要为此写入类似data['address']
。
客户端中的GET
请求正确吗? 我们仅在POST请求成功的情况下进行。
根据您的代码,如果没有发生错误,它应该在执行post请求后返回数据。 因此,我建议您执行以下操作以检查过帐请求是否已成功处理:
f12
,检查“ 网络”选项卡,然后查看请求进程响应,如果响应为空,则执行下一步; generateAddress
方法中设置一个断点 ,然后逐步调试,看看会发生什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.