我有一个带有 boolean (不是原始 boolean )属性的 bean。这是因为该属性并不与此类的每个实例相关,因此应该是可为空
。
Bean 在创建 REST 服务上作为 JSON 发送。 Controller 接收 null
而不是实际值。
我的 Controller :
@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);
}
}
我的 bean :
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,“true”,{“value”:true}。我错过了什么?
最佳答案
添加@JsonProperty到字段:
@JsonProperty("active")
private Boolean active
Marker annotation that can be used to define a non-static method as a "setter" or "getter" for a logical property (depending on its signature), or non-static object field to be used (serialized, deserialized) as a logical property.
如果不起作用,请在 createTransaction
方法中的 MyBeanDTO
之前删除 @RequestBody
关于java - 通过 JSON 发送 java.lang.Boolean (Spring Boot),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53692403/