0

What I want to do:

I want to pass on two parameters from front-end to the back-end.

This is my code:

In TS-file:

activeFromActiveToQuery(req?: any): Observable<ResponseWrapper>{
        const options = createRequestOption(req);
        options.params.append("active_from", req.active_from.toString());
        options.params.append("active_To", req.active_to.toString());

        return this.http.get(this.resourceActiveFromActiveToURL, options)
            .map((res: Response) => this.convertResponse(res));
    }

checkOverlappingDates (recommendedSection: RecommendedSection) {
        this.activeFromActiveToQuery({
            active_from: recommendedSection.activeFrom,
            active_to: recommendedSection.activeTo
        }).subscribe((data) => {
            var retValue;
            if(data.json == ""){
                retValue = false;
            }else{
                retValue = true;
            }
            return retValue;

In Java-Resource class:

public ResponseEntity<List<RecommendedSection>> getRecommendedSectionActiveFromAndActiveToMatching(@RequestParam("active_from") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) DateTime active_from, 
                                                                                                       @RequestParam("active_to") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) DateTime active_to){
        log.debug("REST request to get active_from and active_to: {}", active_from, active_to);
        List<RecommendedSection> entityList = recommendedSectionService.findRecommendedSectionActiveFromAndActiveToMatching(active_from, active_to);
        return ResponseEntity.ok().body(entityList);
    }

The Problem:

I get a Bad Request which tells me the following:

"Failed to convert value of type 'java.lang.String' to required type 'org.joda.time.DateTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.validation.Valid @org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat org.joda.time.DateTime] for value '[object Object]'; nested exception is java.lang.IllegalArgumentException: Invalid format: "[object Object]""

Im sorry if this is a really simple one to solve. I simply couldn't find a solution that worked for me.

But any idea how to solve this?

matsch
  • 281
  • 4
  • 14
  • Have you checked this? Looks quite similar: https://stackoverflow.com/questions/40274353/how-to-use-localdatetime-requestparam-in-spring-i-get-failed-to-convert-string – Gynteniuxas Oct 12 '20 at 08:55
  • @GytisTG Yes, I used it trying to fix it, but it still doesn't work. – matsch Oct 12 '20 at 09:44
  • First thing first, in your log you are passing to arguments and you have only single placeholder. It is not related to your problem but just FYI – Vasif Oct 12 '20 at 11:35

1 Answers1

1

As you are using ISO datetime format, please be sure your request parameter (active_from and active_to) is in ISO dateTime format : yyyy-MM-dd'T'HH:mm:ss.SSSXXX e.g. 2020-10-12T00:01:01 is a good example value. Additionally instead of Joda dateTIme I'd suggest using LocalDateTime

Vasif
  • 668
  • 4
  • 10
  • Thank you! Switched from DateTime to LocalDateTime and it worked. – matsch Oct 13 '20 at 07:38
  • I am glad for you. LocalDateTIme is new and thread-safe because it is immutable so always try to prefer to use it in new code. – Vasif Oct 13 '20 at 09:00