我有一个具有Boolean(不是基本布尔值)属性的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);
}
}
我的豆子:
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都不正确解析属性“活动”,并且始终以null
的形式出现。我已经尝试过true,“ true”,{“ value”:true}。我想念什么?
答案 0 :(得分:1)
将@JsonProperty添加到字段:
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
<scope>runtime</scope>
</dependency>
标记注释,可用于将非静态方法定义为逻辑属性(取决于其签名)或要使用的非静态对象字段(序列化,反序列化)的“设置者”或“获取者”作为逻辑属性。
如果不起作用,请通过@JsonProperty("active")
private Boolean active
方法在@RequestBody
之前删除MyBeanDTO