0

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? and also how to go to the next element when we use 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);
         }
 }
Bob
  • 86
  • 1
  • 12
  • debug your application put breakpoint on `alertRepository.save(alert);` and see what spring gets as your object. – mavriksc Jun 07 '18 at 15:14
  • I got this: Caused by: org.mariadb.jdbc.internal.util.dao.QueryException: Column 'last_update' cannot be null – Bob Jun 07 '18 at 15:24

3 Answers3

0

You are doing everything correctly but just aren't doing anything to your empty instance of alert and therefore aren't posting anything. 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

NickDim
  • 17
  • 3
0

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.

mavriksc
  • 1,130
  • 1
  • 7
  • 10
0

Have you tried to use:

@Column(nullable=false)
@Temporal(TemporalType.TIMESTAMP)
@LastModifiedDate // this automatically generates a new date when the object is changed.
private Date updatedAt;