我有对象 Bill,其中我 lisen @JsonRootName(value = "bill")。 我有结果 Bill{siteId='null', billId='null'} 我在哪里犯了错误? 我的 json
{
"bill":
{
"siteId":"gkfhuj-00",
"billId":"11b0309c-42b8-4d20-bd58-3e854f039287"
}
}
我的类(class)比尔
@JsonRootName(value = "bill")
public class Bill {
private final String siteId;
private final String billId;
public Bill(String siteId, String billId) {
this.siteId = siteId;
this.billId = billId;
}
public String getSiteId() {
return siteId;
}
public String getBillId() {
return billId;
}
@Override
public String toString() {
return "Bill{" +
"siteId='" + siteId + '\'' +
", billId='" + billId + '\'' +
'}';
}
}
我使用 Json 对象的方法
@PostMapping("/json")
@ResponseBody
public ResponseEntity getJson(@RequestBody Bill bill) {
System.out.println(bill.toString());
return null;
}
最佳答案
问题可能是 jackson 的“功能切换”。您需要启用UNWRAP_ROOT_VALUE在您的ObjectMapper
中。因此,如果您使用 Spring,这应该进入您的 @Bean
配置中的某个位置:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
关于java - 无法读取 json post 方法 (jackson),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57231434/