java - @RequestBody 变量返回 null 属性

标签 java spring jackson javabeans

我编写了一个 Controller ,用于接收发布 json 的发布请求,但是当我尝试访问对象字段时,它返回 null。 Controller 的代码如下

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

当我发送下面的请求时,我的代码打印 null

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

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

最佳答案

您可以通过在字段上方使用 @JsonProperty 注释来解决此问题,如下所示:

...
@JsonPropety(value = "Amount")
private Double amount;
...

或者,您可以将属性重命名为以小写字母开头(在虚拟机和传入的 json 中均如此),如评论中的 @OrangeDog 所建议。

关于java - @RequestBody 变量返回 null 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52871109/

相关文章:

java - 使用Ant递归删除一种类型的所有文件

java - 如何为 Web 应用程序配置 log4j.xml?

java - 如何在 Spring 中创建数据更改表单

java - 在 Tomcat 中作为独立运行 Spring Hibernate 项目,在 STS IDE 中运行嵌入式 Tomcat

java - 如何将以下 json 字符串转换为 java 对象?

java - 使用 Spring Hateoas 的 jackson 和 Jackson2HalModule 反序列化 json 时为空 id 属性

java - 即使 hashcode() 相同,HashMap<Node, Integer> get 也会返回 null

java - 在 hibernate 中进行逆向工程时不生成对外关系

spring - PROPAGATION_NESTED vs PROPAGATION_ Spring 需要吗?

java - 使 Jackson 在 JSON 中的重复属性上失败