请求不合法,在调用GET请求后发生。

huangapple go评论77阅读模式
英文:

Bad Request after calling GET Request

问题

以下是翻译的代码部分:

在 TS 文件中:

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;
    }
}

在 Java 资源类中:

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);
}

若有任何进一步的问题,请随时提问。

英文:

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&lt;ResponseWrapper&gt;{
        const options = createRequestOption(req);
        options.params.append(&quot;active_from&quot;, req.active_from.toString());
        options.params.append(&quot;active_To&quot;, req.active_to.toString());

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

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

In Java-Resource class:

public ResponseEntity&lt;List&lt;RecommendedSection&gt;&gt; getRecommendedSectionActiveFromAndActiveToMatching(@RequestParam(&quot;active_from&quot;) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) DateTime active_from, 
                                                                                                       @RequestParam(&quot;active_to&quot;) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) DateTime active_to){
        log.debug(&quot;REST request to get active_from and active_to: {}&quot;, active_from, active_to);
        List&lt;RecommendedSection&gt; 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: &quot;[object Object]&quot;"

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?

答案1

得分: 1

由于您正在使用ISO日期时间格式,请确保您的请求参数(active_from和active_to)是ISO日期时间格式:yyyy-MM-dd'T'HH:mm:ss.SSSXXX
例如,2020-10-12T00:01:01是一个很好的示例值。另外,建议使用LocalDateTime,而不是Joda dateTime。

英文:

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&#39;T&#39;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

huangapple
  • 本文由 发表于 2020年10月12日 16:53:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64314588.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定