Spring ClassCastException?
Spring ClassCastException?
java.lang.ClassCastException: class java.lang.Double 无法转换为 class java.lang.Integer (java.lang.Double 和 java.lang.Integer 在模块中java.base 加载程序 'bootstrap')
//using lombok
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Quiz {
@Id
@Column
int id;
@Column
String title;
@Column
String text;
@ElementCollection
@Column
List<String> options;
@ElementCollection
@Column
List<Integer> answer = new ArrayList<>(); //this is obviously a list of Integers
}
@Service
public class QuizService {
@Autowired
Repo repo;
public List<Quiz> getAllQuiz(){
List<Quiz> quizList = new ArrayList<>();
repo.findAll().forEach(quiz -> quizList.add(quiz));
return quizList;
}
public void saveNewQuiz(Quiz quiz){
repo.save(quiz); //error occurred here
}
public void deleteQuiz(Quiz quiz){
repo.delete(quiz);
}
}
import engine.Quiz;
import org.springframework.data.repository.CrudRepository;
public interface Repo extends CrudRepository<Quiz, Integer> {
}
@PostMapping(value = "/api/quizzes", produces = "application/json")
@ResponseBody
public String newQuiz(@RequestBody String json){
Import();
Gson gson = new Gson();
JsonObject jObj = new JsonObject();
try {
jObj = new JsonParser().parse(json).getAsJsonObject();
}catch(Exception e){
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
}
//test out the json
requestHandler(jObj);
Quiz quiz = new Quiz();
int id = idGenerator();
quiz.setId(id);
quiz.setTitle(jObj.get("title").getAsString());
quiz.setText(jObj.get("text").getAsString());
quiz.setOptions(gson.fromJson(jObj.get("options").getAsJsonArray(), List.class));
try {
List<Integer> list = gson.fromJson(jObj.get("answer").getAsJsonArray(), List.class);
quiz.setAnswer(list);
}catch(Exception ignored) {}
quizzes.add(quiz);
service.saveNewQuiz(quiz); //error occured here
return getQuiz(id);
}
我什至从未接触过 Double,怎么会有从 Double 到 Integer 的 class 转换异常?
我不能做的事情:
- 我无法将类型从 Integer 更改为 Double,因为项目
需要整数才能运行。
- 我无法更改数据库,因为该项目需要 h2 数据库
这个异常让我很困惑。
我就是这样解决这个问题的;
首先,我没有从 json 获取列表,而是尝试获取一个 int[],使用它并转换为列表
java.lang.ClassCastException: class java.lang.Double 无法转换为 class java.lang.Integer (java.lang.Double 和 java.lang.Integer 在模块中java.base 加载程序 'bootstrap')
//using lombok
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Quiz {
@Id
@Column
int id;
@Column
String title;
@Column
String text;
@ElementCollection
@Column
List<String> options;
@ElementCollection
@Column
List<Integer> answer = new ArrayList<>(); //this is obviously a list of Integers
}
@Service
public class QuizService {
@Autowired
Repo repo;
public List<Quiz> getAllQuiz(){
List<Quiz> quizList = new ArrayList<>();
repo.findAll().forEach(quiz -> quizList.add(quiz));
return quizList;
}
public void saveNewQuiz(Quiz quiz){
repo.save(quiz); //error occurred here
}
public void deleteQuiz(Quiz quiz){
repo.delete(quiz);
}
}
import engine.Quiz;
import org.springframework.data.repository.CrudRepository;
public interface Repo extends CrudRepository<Quiz, Integer> {
}
@PostMapping(value = "/api/quizzes", produces = "application/json")
@ResponseBody
public String newQuiz(@RequestBody String json){
Import();
Gson gson = new Gson();
JsonObject jObj = new JsonObject();
try {
jObj = new JsonParser().parse(json).getAsJsonObject();
}catch(Exception e){
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
}
//test out the json
requestHandler(jObj);
Quiz quiz = new Quiz();
int id = idGenerator();
quiz.setId(id);
quiz.setTitle(jObj.get("title").getAsString());
quiz.setText(jObj.get("text").getAsString());
quiz.setOptions(gson.fromJson(jObj.get("options").getAsJsonArray(), List.class));
try {
List<Integer> list = gson.fromJson(jObj.get("answer").getAsJsonArray(), List.class);
quiz.setAnswer(list);
}catch(Exception ignored) {}
quizzes.add(quiz);
service.saveNewQuiz(quiz); //error occured here
return getQuiz(id);
}
我什至从未接触过 Double,怎么会有从 Double 到 Integer 的 class 转换异常?
我不能做的事情:
- 我无法将类型从 Integer 更改为 Double,因为项目 需要整数才能运行。
- 我无法更改数据库,因为该项目需要 h2 数据库
这个异常让我很困惑。
我就是这样解决这个问题的; 首先,我没有从 json 获取列表,而是尝试获取一个 int[],使用它并转换为列表