我正在研究REST项目。向端点发出POST请求时,它将接收空值。 这是该课程的摘录:
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/STK")
public Response STK(STKUtil stkutil) throws PSQLException, SQLException {
Body body = stkutil.getBody();
StkCallback stkcallback = new StkCallback();
String merchantRequestID=stkcallback.getMerchantRequestID();
String checkoutRequestID = stkcallback.getCheckoutRequestID();
int resultCode = stkcallback.getResultCode();
String resultDesc = stkcallback.getResultDesc();
System.out.println("MerchantRequestID::"+merchantRequestID);
System.out.println("CheckoutRequestID::"+checkoutRequestID);
System.out.println("ResultCode::"+resultCode);
System.out.println("ResultDesc::"+resultDesc);
String stkcallBackResponse = "{\"ResultDesc\":\"Confirmation received succesfully\",\"ResultCode\":\"0\"}";
System.out.println(stkcallBackResponse);
return Response.status(201).entity(stkcallBackResponse).build();
}
这是具有getter和setter方法的课程:
public class STKUtil {
Body BodyObject;
// Getter Methods
public Body getBody() {
return BodyObject;
}
// Setter Methods
public void setBody(Body BodyObject) {
this.BodyObject = BodyObject;
}
}
public class Body {
StkCallback StkCallbackObject;
// Getter Methods
public StkCallback getStkCallback() {
return StkCallbackObject;
}
// Setter Methods
public void setStkCallback(StkCallback stkCallbackObject) {
this.StkCallbackObject = stkCallbackObject;
}
}
public class StkCallback {
private String MerchantRequestID;
private String CheckoutRequestID;
private String ResultCode;
private String ResultDesc;
CallbackMetadata CallbackMetadataObject;
// Getter Methods
public String getMerchantRequestID() {
return MerchantRequestID;
}
public String getCheckoutRequestID() {
return CheckoutRequestID;
}
public String getResultCode() {
return ResultCode;
}
public String getResultDesc() {
return ResultDesc;
}
public CallbackMetadata getCallbackMetadata() {
return CallbackMetadataObject;
}
// Setter Methods
public void setMerchantRequestID(String MerchantRequestID) {
this.MerchantRequestID = MerchantRequestID;
}
public void setCheckoutRequestID(String CheckoutRequestID) {
this.CheckoutRequestID = CheckoutRequestID;
}
public void setResultCode(String ResultCode) {
this.ResultCode = ResultCode;
}
public void setResultDesc(String ResultDesc) {
this.ResultDesc = ResultDesc;
}
public void setCallbackMetadata(CallbackMetadata CallbackMetadataObject) {
this.CallbackMetadataObject = CallbackMetadataObject;
}
}
public class CallbackMetadata {
ArrayList < Object > Item = new ArrayList < Object > ();
// Getter Methods
// Setter Methods
}
这是我要发布到端点的json请求:
{
"Body":
{"stkCallback":
{
"MerchantRequestID": "21605-295434-4",
"CheckoutRequestID": "2017184930742",
"ResultCode": "0",
"ResultDesc": "The service request is processed successfully.",
"CallbackMetadata":
{
"Item":
[
{
"Name": "Amount",
"Value": 1
},
{
"Name": "ReceiptNumber",
"Value": "T6JSJJS"
},
{
"Name": "Balance"
},
{
"Name": "TransactionDate",
"Value": 20171104184944
},
{
"Name": "PhoneNumber",
"Value": 777727894083
}
]
}
}
}
}
以下是成功发布后打印出来的回复:
Info: MerchantRequestID::null
Info: CheckoutRequestID::null
Info: ResultCode::0
Info: ResultDesc::null
Info: UUID::f7cdc310-dcdb-445a-bc75-5e207b703415
Info: {"ResultDesc":"Confirmation received succesfully","ResultCode":"0"}
我很困惑为什么要发布 null 值。