I wrote a controller for receiving a post request that posts json however when i try to access the object fields it returns null. The code for the Controller is below
package com.example;
import com.example.Services.ZesaServices;
import com.example.models.Zesa.ZesaRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MoneyResource {
@RequestMapping("/services")
public String getServices(){
return "We do everything";
}
@PostMapping(value="/zesa")
public String payZesa(@RequestBody ZesaRequest newZesaRequest){
Logger log = LoggerFactory.getLogger(YomoneyResource.class);
log.info("Json Object parsed works eg: "+newZesaRequest.getMeterNumber());
return newZesaRequest.getMeterNumber().toString();
}
}
And the ZesaRequest Object is as follows
package com.example.models.Zesa;
public class ZesaRequest {
private Double Amount;
private String MeterNumber;
private String PaymentAccountNumber;
private String PaymentAccountDetails;
private int PaymentMethod;
private String MobileNumber;
private String AgentAccountDetails;
private int TransactionType;
public ZesaRequest() {
}
public ZesaRequest(Double amount, String meterNumber, String paymentAccountNumber, String paymentAccountDetails,
int paymentMethod, String mobileNumber, String agentAccountDetails, int transactionType) {
this.Amount = amount;
this.MeterNumber = meterNumber;
this.PaymentAccountNumber = paymentAccountNumber;
this.PaymentAccountDetails = paymentAccountDetails;
this.PaymentMethod = paymentMethod;
this.MobileNumber = mobileNumber;
this.AgentAccountDetails = agentAccountDetails;
this.TransactionType = transactionType;
}
public String getPaymentAccountDetails() {
return PaymentAccountDetails;
}
public void setPaymentAccountDetails(String paymentAccountDetails) {
PaymentAccountDetails = paymentAccountDetails;
}
public String getMobileNumber() {
return MobileNumber;
}
public void setMobileNumber(String mobileNumber) {
MobileNumber = mobileNumber;
}
public Double getAmount() {
return Amount;
}
public void setAmount(Double amount) {
Amount = amount;
}
public String getMeterNumber() {
return MeterNumber;
}
public void setMeterNumber(String meterNumber) {
MeterNumber = meterNumber;
}
public String getPaymentAccountNumber() {
return PaymentAccountNumber;
}
public void setPaymentAccountNumber(String paymentAccountNumber) {
PaymentAccountNumber = paymentAccountNumber;
}
public int getPaymentMethod() {
return PaymentMethod;
}
public void setPaymentMethod(int paymentMethod) {
PaymentMethod = paymentMethod;
}
public String getAgentAccountDetails() {
return AgentAccountDetails;
}
public void setAgentAccountDetails(String agentAccountDetails) {
AgentAccountDetails = agentAccountDetails;
}
public int getTransactionType() {
return TransactionType;
}
public void setTransactionType(int transactionType) {
TransactionType = transactionType;
}
}
My code is printing null When I send the request below
{
"AgentAccountDetails":"example:123",
"MeterNumber":"1110-52-8867",
"PaymentMethod":1,
"Amount":10.50,
"MobileNumber":"0123456789",
"TransactionType":1,
"PaymentAccountNumber":"0123456789",
"PaymentAccountDetails":"null"
}
When I run it it returns an empty String. I am not sure where the problem is I have looked into other examples and they followed a similar pattern and I ran their code and it worked as expected but mine seems to not be converting the json body into the Java object.