Java 使用@JsonAlias使用@RequestBody转换为Pojo

Java 使用@JsonAlias使用@RequestBody转换为Pojo,java,json,Java,Json,我有下面给出的JSON模式 { "prty_id": "hgh", "customer_id": "ghjghjghj" } 我有下面给出的模型课 public class PARTY { @JsonAlias({"prty_id", "prtyId"}) String prtyId; @JsonAlias({"customer_id", "customerId"}) String customerId; ...... } 当我试

我有下面给出的JSON模式

{
    "prty_id": "hgh",
    "customer_id": "ghjghjghj"
}
我有下面给出的模型课

public class PARTY   {
    @JsonAlias({"prty_id", "prtyId"})
    String prtyId;

    @JsonAlias({"customer_id", "customerId"})
    String customerId;
    ......
}
当我试图使用
@RequestBody
注释,将
@RequestBody
参与方主体作为方法参数进行反序列化时,我得到的POJO类字段值为空

@PostMapping(value="/party/db",produces="application/json",consumes="application/json")


public Party controller(@RequestBody Party body)
//here in body parameter only i am getting null values
                {
                    ObjectMapper obj = new ObjectMapper();
                    String json = obj.writeValueAsString(body);
                    System.out.println("===="+json);

                    PARTY p = obj.readValue(json, PARTY.class);
                    System.out.println("------"+p);
                    p.toString();
                    return p;
                }

你需要在你的课堂上写下getter,这样jackson的去Elialization就可以工作了。在您的定义中,还有更好的用例:

public class Party {

    @JsonAlias({"prty_id", "prtyId"})
    String prtyId;

    @JsonAlias({"customer_id", "customerId"})
    String customerId;

    public String getPrtyId() {
        return prtyId;
    }

    public String getCustomerId() {
        return customerId;
    }
}
编辑:添加控制器:

@PostMapping("/test")
public Party test(@RequestBody Party body) {
    return body;
}

我建议将
@JsonProperty
@jsonaas
一起使用。试试看

@JsonProperty
@JsonAlias(xxx,xxx)
私有字符串xxx;

就是这样。乍一看,代码应该可以工作

“我没有得到空值”这是正确的还是应该是“我得到空值”?@微笑是的,它应该是空的错误我写了不是空的我提供了setter和getter,但我也得到了空值我刚刚检查了你的输入json与我写的一个控制器(在答案中添加),它工作得很好。也许你没有正确调用你的控制器。我添加了控制器代码。我没有得到代码正常工作所需的输出。请显示您的http请求,那里一定有问题。您添加了内容类型标题了吗?我正在使用postman点击url,是的,添加了内容类型标题,其值为application/json