我有一个带有布尔(非原始布尔)属性的bean . 这是因为该属性与此类的每个实例都不相关,因此应该是 nullable
.
Bean在创建REST服务上作为JSON发送 . 控制器接收 null
而不是实际值 .
我的控制器:
@RestController
@RequestMapping("/myBean")
public class MyBeanController {
@Autowired
private MyBeanService myBeanService;
@PostMapping("/create" )
public ResponseEntity createTransaction(@RequestBody MyBeanDTO myBean) {
MyBeanDTO result = myBeanService.create(myBean);
return new ResponseEntity(result, HttpStatus.OK);
}
}
我的 beans 子:
public class MyBean {
. . .
private Boolean active;
. . .
public Boolean getActive() { //Instead of isActive, as it's Boolean and not boolean
return active;
}
public void setActive(Boolean active) {
this.active = active;
}
}
我发送的JSON都没有正确解析属性"active",并始终为 null
. 我试过了,"true",{"value":true} . 我错过了什么?
1 回答
将@JsonProperty添加到字段:
如果不起作用,请在
createTransaction
方法中的MyBeanDTO
之前删除@RequestBody