@RequestBody变量返回空属性

时间:2018-10-18 09:33:37

标签: java spring jackson javabeans

我写了一个控制器来接收发布请求,该请求发布json,但是当我尝试访问对象字段时,它返回null。控制器的代码如下

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();
  }
}

ZesaRequest对象如下

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;
}
}

我的代码显示为空当我在下面发送请求时

{
   "AgentAccountDetails":"example:123",
   "MeterNumber":"1110-52-8867",
   "PaymentMethod":1,
   "Amount":10.50,
   "MobileNumber":"0123456789",
   "TransactionType":1,
   "PaymentAccountNumber":"0123456789",
   "PaymentAccountDetails":"null"
}

当我运行它时,它返回一个空字符串。我不确定问题出在哪里,我研究了其他示例,它们遵循类似的模式,我运行了它们的代码,并按预期工作,但我的似乎没有将json主体转换为Java对象。

2 个答案:

答案 0 :(得分:1)

您的类定义了一个名为meterNumber的{​​{3}},但您的JSON对象却改为MeterNumber

如果您的JSON中必须包含MeterNumber,则需要添加property批注。

以大写字母开头的字段名称违反Java和JSON命名约定。

顺便说一句,您可以使用@JsonProperty来避免所有重复的事情:

@Data
public class ZesaRequest {
    @JsonProperty("Amount")
    private Double amount;

    @JsonProperty("MeterNumber")
    private String meterNumber;

    @JsonProperty("PaymentAccountNumber")
    private String paymentAccountNumber;

    @JsonProperty("PaymentAccountDetails")
    private String paymentAccountDetails;

    @JsonProperty("PaymentMethod")
    private int paymentMethod;

    @JsonProperty("MobileNumber")
    private String mobileNumber;

    @JsonProperty("AgentAccountDetails")
    private String agentAccountDetails;

    @JsonProperty("TransactionType")
    private int transactionType;
}

您可能也不想使用"PaymentAccountDetails":"null"。它应该为"PaymentAccountDetails":null,或完全省略。

答案 1 :(得分:1)

您可以使用以下字段上方的import ssl orig_sslsocket_init = ssl.SSLSocket.__init__ ssl.SSLSocket.__init__ = lambda *args, cert_reqs=ssl.CERT_NONE, **kwargs: orig_sslsocket_init(*args, cert_reqs=ssl.CERT_NONE, **kwargs) 注释来解决此问题:

@JsonProperty

或者您也可以按照注释中的... @JsonPropety(value = "Amount") private Double amount; ... 重命名属性,使其以小写字母开头(在VM和传入的json中都是这样)。