Home > Net >  How to pass array of dates from angular to java
How to pass array of dates from angular to java

Time:08-26

I'm sending an array holding objects wich hold a date-attribute from angular to spring rest api. In the rest API this attribute is a LocalDate. Java takes all the the date attributes as null objects. How can I make sure the values get passed correct?

I've found this similar topic:

I have tried converting the date into a string which did not help. My question is different since I want to send an array holding objects wich have a date attribute. So I cannot just send 1 date using params. Here below I posted the angular models and the service:

export interface DaysOfMonth {
    dayType: DayTypes;
    workingHours: number;
    date: Date;
}

export interface MonthObject {
    year: number;
    month: number;
    daysOfMonth: DaysOfMonth[];
}

export interface TimeSheet {
    id: number;
    username: string;
    status: Status;
    year: number;
    month: number;
    monthObject: MonthObject;
}

service:

@Injectable({
  providedIn: 'root'
})
export class TimesheetService {

  constructor(private http: HttpClient, private datePipe: DatePipe) { }


  updateDaytype(sheet: TimeSheet): Observable<TimeSheet>{
    console.log("updateDaytype reached")
    const url = environment.TIMESHEETSAPI_URL   "timesheets/updateSheetById";
    return this.http.put<TimeSheet>(url, sheet);
  }
}

CodePudding user response:

Java LocalDate is a date without any time info and no timezone info - just the information of the date, such as 12th August of 2022. Now the Spring Server and the Angular Client have to agree on a common format to communicate this information. In JSR-310 this is in the date format of "yyyy-MM-dd". In order to enforce Spring to use this type of Serialization/Deserialization when faced with JSON you have to configure it that way.

// add depedency
compile ("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")

And add this to your DTO

@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
private LocalDate date;

Now on the Angular part you have to convert your Date object to adhere to the format, too. Therefore, maybe do something like this with day.js:

Date.prototype.toJSON = function() {
  return dayjs(d).format("yyyy-MM-dd")
};

Note that my solution is just a dirty approach and not tested.

CodePudding user response:

It is not possible to simply send Angular Date objects directly to your Java API like that, you have to do the following: You have to take the array and use the quicksort algorithm on them and then hash hem using SHA-256, once you got your hash, you must take the String from 7th character till the 13th character and then convert that new String to binary code according to the IO-6078 standards. Once you've got your binary code you must convert it into assembly code according to your manufacturers specifications of your device and inject that code into the transistors at memory location Ux64960 till Ux65005. If you owe a Mac it is the memory Ux38555 till Ux38720. Make sure your GPU is getting atleast 13.8 volts when doing this as otherwise it will not work. This will result in an overload of electricity at the Data service layer which will secretly pass the array through the ethernet cable to your java api as it otherwise would've been caught by the Angular Apache Service (APS) had it gone through the web services.

  • Related