[英]How to send a specific POST request to an API using Spring Boot
I want to create a Post method for my controller for my Spring boot application, I have an Entity Alert, with the elements below, but I need to send a specific request format to my API, I tried using save() method but it doesn't work, does someone knows how to specify each element for the Json request? 我想为我的Spring启动应用程序的控制器创建一个Post方法,我有一个带有以下元素的Entity Alert,但是我需要向我的API发送特定的请求格式,我尝试使用save()方法,但是它没有没有用,有人知道如何为Json请求指定每个元素吗? and also how to go to the next element when we use Serializable? 以及当我们使用Serializable时如何转到下一个元素? Thank you so much for help 非常感谢您的帮助
{
"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;
And for my controller: 对于我的控制器:
@RestController
@RequestMapping("/api/alerts")
public class AlertsController extends AbstractController{
@Autowired
private AlertRepository alertRepository;
@PostMapping()
public void createAlert(@RequestBody Alert alert) {
alertRepository.save(alert);
}
}
You are doing everything correctly but just aren't doing anything to your empty instance of alert and therefore aren't posting anything. 您可以正确执行所有操作,但是对空的Alert实例没有执行任何操作,因此也没有发布任何内容。 In order to post something you would need to do something like alert.setSomething(something) and then alertRepository.save(alert) because you are currently saving nothing 为了发布内容,您需要先执行alert.setSomething(something)之类的操作,然后再执行alertRepository.save(alert)之类的操作,因为您当前没有保存任何内容
most likely you need to create correct device objects. 您很可能需要创建正确的设备对象。
{
devices:[
{
"deviceEUIs": "1122233344"
},{
"deviceEUIs": "1122233355"}
]
}
for the error you posted in the comment above. 您在上述评论中发布的错误。 add this to entity. 将此添加到实体。
@PrePersist
@PreUpdate
public void onUpdate() {
lastUpdate= new Date();
}
there is a solution here that shows you how you can use this in a super class and inherit all classes you want to have those fields on. 有一个解决方案在这里将告诉您如何在一个超类使用和继承你想对这些领域的所有类。
Have you tried to use: 您是否尝试过使用:
@Column(nullable=false) @Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP) @Temporal(TemporalType.TIMESTAMP)
@LastModifiedDate // this automatically generates a new date when the object is changed. @LastModifiedDate // //更改对象时,它会自动生成一个新日期。
private Date updatedAt; 私人DateupdateAt;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.