简体   繁体   English

如何使用Spring Boot向API发送特定的POST请求

[英]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.

相关问题 Spring 引导 - 如何使用 Post 请求 (JSON) 发送对象数组 - Spring Boot - How to send an array of objects using Post request (JSON) 如何在同一个 POST 请求中发送 JSON 和图像并将其级联? Spring 开机 - How to send JSON and an Image in same POST request and cascade it? Spring Boot 如何在Spring Boot Post请求中发送对象数组 - how to send array of objects in spring boot post request 使用 Angular 2 和 Spring Boot 发送 post 请求 - Send post request with Angular 2 and Spring Boot 发送请求到Spring Rest API - Send post request to spring rest api 使用Spring Boot和AWS的简单POST请求 - Simple POST request using Spring Boot and AWS 如何在Spring Boot框架中发送HTTP请求? - How to send a HTTP request in Spring boot framework? Spring Boot - 如何通过带有查询参数的 url 发送 POST 请求并将响应存储在方法中? - Spring Boot - How to send a POST request via a url with query parameters and store the response inside a method? 对于 Spring Boot 中的 GET 请求,如何在单个 JSON 响应中发送 Header 中的一些特定信息和 Body 中的一些信息? - How to send some specific info in Header and some info in Body in a single JSON response, for a GET request in Spring boot? Spring Boot POST请求特定的有效负载不被接受 - Spring Boot POST Request specific payload not being accepted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM