英文:
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<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?
答案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'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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论