gpt4 book ai didi

javascript - 将 Javascript 数组匹配到 Java 列表

转载 作者:行者123 更新时间:2023-12-05 06:49:46 25 4
gpt4 key购买 nike

我正在从前端 (Angular) 向后端 (Java) 发出 POST 请求。 post 请求作为 JSON 对象(由多个数组组成)发送,在 Java 中,应与数组旨在成为列表的对象匹配。这是一个代表问题的示例代码,因为这不是实际代码,因为它属于工作并且不希望有任何 secret 性问题,所以这只是为了尽可能接近原始代码来代表问题(值得注意的是有很多存储的学校,为了争论起见,他们可以由他们的学生和所教授的类(class)唯一标识,并使用此信息,我们希望获得该学校的教室):

Angular 服务 API 调用:

getSchool(students, lessons): Promise<School> {
return this.http.post('sampleurl/classroom', JSON.stringify({students:students, lessonsTaught:lessons}), {headers:this.headers})
.toPromise()
.then(res => <School>res.json())
.catch(this.handleError);
}

Angular 学校模型:

export class School {
public students:any;
public lessonsTaught:string[];
public classrooms:string[];

constructor() {}
}

Java 学校对象:

public class School {
private List<Student> students;
private List<String> lessonsTaught;
private List<String> classrooms;

public School() {}

public List<Student> getStudents() {
return students;
}

public void setStudents(List<Student> students) {
students = this.students;
}

public List<Stream> getLessonsTaught() {
return students;
}

public void setLessonsTaught(List<String> lessonsTaught) {
lessonsTaught = this.lessonsTaught;
}

public List<String> getClassrooms() {
return classrooms;
}

public void setClassroom(List<String> classroom) {
classroom = this.classroom;
}

Java SearchSchool 对象:

public class SearchSchool {
private List<String> students;
private List<String> lessonsTaught;

public School() {}

public List<Student> getStudents() {
return students;
}

public void setStudents(List<Student> students) {
students = this.students;
}

public List<Stream> getLessonsTaught() {
return students;
}

public void setLessonsTaught(List<String> lessonsTaught) {
lessonsTaught = this.lessonsTaught;
}

Java 端点:

@RequestMapping(value="sampleurl/classroom", method = RequestMethod.POST)
public School(@RequestBody SearchSchool searchSchool) {
return service.getSchool(searchSchool);
}

如此有效,我使用 Java 中的 SearchSchool 对象来唯一标识学校并返回它。但是,传递给端点的数组似乎与 SearchSchool 对象所需的列表不匹配(我在端点的条目上放置了一个断点,它甚至没有被执行)。稍微尝试了一下代码后,Angular 中学生的字符串数组与 Java 中学生的字符串列表的匹配似乎工作正常,问题似乎是将所教授的类(class)数组与所教授的类(class)列表相匹配在 Chrome 中的开发者控制台给我以下运行时错误:

An error occurred syntaxError: service.ts: 163
Unexpected token A in JSON at position o
at JSON.parse (<anonymous>)
at Response.Body.json (http.ess.js:796)
at duration service.ts:54
at ZoneDelegate.invoke (zone.js:365)
at object.onInvoke (core.ess.js:4145)
at ZoneDelegate.invoke (zone.js:364)
at zone.run (zone.js:125)
at zone.js:760
at ZoneDelegate.invokeTask (zone.js:398)
at object.on InvokeTask (core.ess.js:4136)
at ZoneDelegate.invokeTask (zone.js:397)
at Zone.runTask (zone.js:165)
at drainMicroTaskQueue (zone.js:593)
at XMLHttpRequest. ZoneTask.invoke (zone.js:464)

和:

ERROR Error: Uncaught (in promise): core.es5.js:1085
Unexpected token A in JSON at position o
at resolvePromise (zone.js:683) [angular]
at :8080/gar-app/polyfis:7034:17 [angular)
at object.onInvokeTask (core.ess.js:4136) [angular]

有谁知道为什么从前端的数组匹配到后端的列表会出现这样的问题?

最佳答案

因为前后端字段名不匹配

在前端你正在传递类(class),在后端你期待 lessonsTaught

修改任何一个以使代码工作

更改后的前端看起来像这样

getSchool(students, lessons): Promise<School> {
return this.http.post('sampleurl/classroom', JSON.stringify({students:students, lessonsTaught:lessons}), {headers:this.headers})
.toPromise()
.then(res => <School>res.json())
.catch(this.handleError);
}

关于javascript - 将 Javascript 数组匹配到 Java 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66529673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com