Reputation: 12441
I work with a Java RESTful
service and the consuming client. The Java app is receiving and delivering the data in the JSON
format. I need to talk about 2 requests and their implementations in the consuming RESTful client
.
If I click a button, a WalletInfo
entity is generated and stored in the database. It has properties like id
, name
, address
and the currency
. The click is essentially a POST
request.
The definition is provided,
@POST
@Path("generateAddress")
@Consumes(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_JSON})
WalletInfo generateAddress(GenerateWallet generateWallet);
This is the implementation,
public synchronized WalletInfo generateAddress(GenerateWallet generateWallet) {
String walletName = generateWallet.getWalletName();
String currencyName = generateWallet.getCurrencyName();
WalletInfo walletInfo = iWalletInfoDao.getWalletInfoWithWalletNameAndCurrency(walletName, currencyName);
return walletInfo;
}
I use this cURL
command to do the POST
request,
curl -H "Content-Type: application/json" -X POST -d '{"walletName": "Rome12345","currencyName":"Bitcoin"}' http://localhost:8080/rest/wallet/generateAddress
As it's the POST
request, obviously, it doesn't return anything in the terminal
. I have another GET
request, the definition is provided,
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("currencyandname/{walletName}/{currencyName}")
WalletInfo getWalletInfoWithCurrencyAndWalletName(@PathParam("walletName") String walletName,
@PathParam("currencyName") String currencyName);
Here is the implementation,
public WalletInfo getWalletInfoWithCurrencyAndWalletName(String walletName, String currencyName) {
return iWalletInfoDao.getWalletInfoWithWalletNameAndCurrency(walletName, currencyName);
}
I do the cURL
request like this for the execution,
curl -X GET http://localhost:8080/rest/wallet/currencyandname/test1/Bitcoin | json
If the name and the currency info matches with the info in the database, it returns the info for the WalletInfo
entity. For example, we provide the name test1
and the currency Bitcoin
using the cURL request and get something like in the terminal,
{
"id": 1,
"name": "test1",
"address": "mp51mPC38Wtcmybdyd9MPEB2bKnw6eYbCs",
"currency": "Bitcoin"
}
If there is no match, we get nothing. And when we get a match, we can use other info, primarily, the address.
Let's talk about the client. As I said earlier, there is a button in the consuming client to generate the address and to fill a drop-down with that address. After the click of the button, the following POST
request will be executed in the client,
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);
}
});
Is the POST request is correct? I mean when I do the POST
CURL command in the terminal, I get nothing in return. But, the success function(data)
seems to need data
as the input parameter after the successful execution of the POST
request. I also have the WalletEntity
as the return type in the Java method.
2nd Question
If the POST
request is successful that means a WalletEntity is generated and stored in the MySQL. Now, I would like to do a GET
request to get the same WalletInfo
entity using the name/ currency name and use the address parameter from that instance.
It needs to be inside the success: function(data)
of the POST
request as we will only do the GET request after successful creation of the wallet and will use the getWalletInfoWithCurrencyAndWalletName
method of the Java app for this purpose. I image the GET
request like something,
$.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()));
}
});
I assume the data is the WalletEntity
and address is one of it's parameters. So, If I would like to only have the address
, I need to write like data['address']
for the purpose.
Is this the GET
request in the client is correct? We only do if the POST request is successful.
Upvotes: 0
Views: 63
Reputation: 136
Accroding to your code, it should return data after do the post request if no error was happen. So i advice you to do these things to check if the post request was processed successfully:
f12
in your browser, check the NetWork tab, and see the
request process response, if the response is empty, them do the next
step;generateAddress
method, and then debug step
by step, see what will happened.Upvotes: 1