Home > database >  I'm getting 400 - bad request when i try to send date in JSON to backend
I'm getting 400 - bad request when i try to send date in JSON to backend

Time:11-23

i'm getting the 400 HTTP error every time I try to send some date object in JSON and i don't know why :(

This is my entity:

@Entity
public class Scheduling implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy HH:mm", timezone = "Brazil/East")
    @DateTimeFormat(pattern = "dd/MM/yyyy HH:mm")
    @Temporal(TemporalType.TIMESTAMP)
    @Column(nullable = false, length = 254)
    private Date initialDate;
    

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy HH:mm", timezone = "Brazil/East")
    @DateTimeFormat(pattern = "dd/MM/yyyy HH:mm")
    @Temporal(TemporalType.TIMESTAMP)
    @Column(nullable = false, length = 254)
    private Date finalDate;

    @ManyToOne
    @JoinColumn(name = "teacher_id")
    private Teacher teacher;

    @ManyToOne
    @JoinColumn(name = "classroom_id")
    private Classroom classroom;

    @ManyToOne
    @JoinColumn(name = "class_id")
    private Class group;
    
    @ManyToMany
    @JoinTable(name = "equipments_schedulings",
            joinColumns = @JoinColumn(name = "scheduling_id"), scheduling_id
            inverseJoinColumns = @JoinColumn(name = "equipment_id")) equipment_id
    private List<Equipment> equipments = new ArrayList<>();

and this is my JSON body:

{
    "initialDate": "22/11/2022 08:00",
    "finalDate": "22/11/2022 08:45",
    "teacher": {
        "id": 1
    },
    "group": {
        "id": 1
    },
    "classroom": {
        "id": 1
    },
    "equipment": [
        {
            "id": 1
        },
        {
            "id": 2
        }
    ]
}

and I'm already have this on my application.properties file:

spring.jackson.time-zone=Brazil/East
spring.jackson.serialization.write-dates-as-timestamps: false

It doesn't show any error on my console and I'm very confused.

If someone know what is happening I'll be very grateful ^^

CodePudding user response:

Basically, we cannot assign timezone with a dynamic or a runtime value. It should be constant at compile time. Even enums are accepted.

So we should assign a constant to timezone.

private static final String TIME_ZONE="Brazil/East";
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy HH:mm", timezone = TIME_ZONE);
  • Related