【发布时间】:2021-10-23 07:41:18
【问题描述】:
我正在尝试为 Spring 中的 get 请求编写处理程序,但出现此错误:
类 java.math.BigInteger 不能转换为类 java.lang.String (java.math.BigInteger 和 java.lang.String 在模块 java.base 中 加载程序“引导程序”)。
请检查我的控制器类
@GetMapping("getListOfStudentsBasedOnTrainerId/{trainerId}")
public ResponseEntity<List<TrainerIdResponse>> getListOfStudentsBasedOnTrainerId( @PathVariable int trainerId)
throws RecordNotFoundException{
List<TrainerIdResponse> trainerIdresp= userService.getListOfStudentsBasedOnTrainerId(trainerId);
return new ResponseEntity<List<TrainerIdResponse>>(trainerIdresp,new HttpHeaders(), HttpStatus.OK);
}
这是用户服务方法
public List<TrainerIdResponse> getListOfStudentsBasedOnTrainerId(@PathVariable int trainerId) throws RecordNotFoundException{
List<Object> trainerIdResponse=searchRepository.getListOfStudentsBasedOnTrainerId(trainerId);
Iterator it =trainerIdResponse.iterator();
List<TrainerIdResponse> trainerIdList =new ArrayList<>();
while(it.hasNext()) {
Object[] row=(Object[])it.next();
TrainerIdResponse trainerIdResponse1= new TrainerIdResponse();
trainerIdResponse1.setStudentid(Integer.valueOf((Integer)row[0]).intValue());
trainerIdResponse1.setStudentname(String.valueOf(row[1]));
trainerIdResponse1.setStudentImage(String.valueOf(row[2]));
trainerIdResponse1.setEnrolleddate(String.valueOf(row[3]));
trainerIdResponse1.setHighereducation(String.valueOf(row[4]));
trainerIdResponse1.setUniversity(String.valueOf(row[5]));
trainerIdResponse1.setEmployement(String.valueOf(row[6]));
trainerIdResponse1.setLocation(String.valueOf(row[7]));
trainerIdResponse1.setTrainerid(Integer.valueOf((Integer)row[8]).intValue());
trainerIdList.add(trainerIdResponse1);
}
return trainerIdList;
}
我的搜索库类
@Query(value ="select u.userid as \"studentid\",u.username as \"studentname\",u.Image_url as \"studentImage\",u.addeddate as \"enrolleddate\", u.highestqualification as \"highereducation\",\r\n"
+ "u.college as \"university\",e.employmenttype as \"employement\",e.location,\r\n"
+ "ur.requestedtoid as \"trainerid\" from users u JOIN userexperience e \r\n"
+ "on u.userid = e.userid join userrequests ur on u.userid = ur.requestedtoid \r\n"
+ "where u.userid = ur.requestedtoid or u.userid =:trainerId",nativeQuery = true)
List<Object> getListOfStudentsBasedOnTrainerId(int trainerId);
我的响应类
import java.util.Date;
public class TrainerIdResponse {
private int studentid;
private String studentname;
private String studentImage;
private String enrolleddate;
private String highereducation;
private String university;
private String employement;
private String location;
private int trainerid;
public int getStudentid() {
return studentid;
}
public void setStudentid(int studentid) {
this.studentid = studentid;
}
public String getStudentname() {
return studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}
public String getStudentImage() {
return studentImage;
}
public void setStudentImage(String studentImage) {
this.studentImage = studentImage;
}
public String getEnrolleddate() {
return enrolleddate;
}
public void setEnrolleddate(String enrolleddate) {
this.enrolleddate = enrolleddate;
}
public String getHighereducation() {
return highereducation;
}
public void setHighereducation(String highereducation) {
this.highereducation = highereducation;
}
public String getUniversity() {
return university;
}
public void setUniversity(String university) {
this.university = university;
}
public String getEmployement() {
return employement;
}
public void setEmployement(String employement) {
this.employement = employement;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getTrainerid() {
return trainerid;
}
public void setTrainerid(int trainerid) {
this.trainerid = trainerid;
}
}
【问题讨论】:
-
请发布您的堆栈跟踪的屏幕截图。您发布的内容似乎不是完整的堆栈跟踪,甚至不是完整的消息。这很可能是您的列是数字的问题,但您的 POJO 类属性是字符串类型(反之亦然)
-
{ "statusCode": 500, "timestamp": "2021-08-22T19:20:16.873+00:00", "message": "方法参数类型所需的请求参数 'trainerId' int 不存在", "description": "uri=/users/getListOfStudentsBasedOnTrainerId/6" }
-
您的错误很可能出现在表达式
(Integer)row[0](和row[8])中。看起来您的数据库将这些字段存储为BigInteger,而不是Integer,但您正试图将其转换为整数。此外,Integer.valueOf((Integer)x)是多余的。相当于(Integer)x。 -
@BharatChaudhari
row[0].getClass()和row[8].getClass()是什么? -
@BharatChaudhari 您正在使用 row[0] 和 row[8],他们要求您使用 getClass() 检查类的内容。您还被要求发布 StackTrace。旁注:为什么标题中的错误与您的帖子正文中的错误不同?
标签: java spring api spring-mvc