【发布时间】:2018-06-07 15:08:54
【问题描述】:
我想为我的 Spring Boot 应用程序的控制器创建一个 Post 方法,我有一个实体警报,包含以下元素,但我需要向我的 API 发送特定的请求格式,我尝试使用 save() 方法但它不起作用,有人知道如何为 Json 请求指定每个元素吗?以及当我们使用 Serializable 时如何转到下一个元素?非常感谢您的帮助
{
"deviceEUIs": [
"1122233344"
],
"alertModes": [
{
"type": "MOTION",
"notifyByEmail": true,
"notifyBySMS": true,
"notifyOnInterface": true
}
]
}
@Entity
@Table(name = "Alert")
public class Alert implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Column(columnDefinition = "BINARY(16)")
private UUID alertRef;
@OneToMany(mappedBy = "alert", cascade = CascadeType.ALL)
private Set<AlertModes> alertModes;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable
private Set<Subscriber> subscribers;
@ManyToMany(mappedBy = "alerts")
private Set<Device> devices;
@Column(name = "last_update", columnDefinition = "DATETIME", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date lastUpdate;
对于我的控制器:
@RestController
@RequestMapping("/api/alerts")
public class AlertsController extends AbstractController{
@Autowired
private AlertRepository alertRepository;
@PostMapping()
public void createAlert(@RequestBody Alert alert) {
alertRepository.save(alert);
}
}
【问题讨论】:
-
调试你的应用程序,在
alertRepository.save(alert);上设置断点,看看spring得到什么作为你的对象。 -
我得到了这个:原因:org.mariadb.jdbc.internal.util.dao.QueryException:列'last_update'不能为空
标签: java json api spring-boot controller