gpt4 book ai didi

java - 不匹配预期类型 spring 框架错误

转载 作者:行者123 更新时间:2023-11-30 11:00:28 25 4
gpt4 key购买 nike

我有一个基于 spring 框架的服务我想知道为什么我在向我的服务发送整数时出现此异常

ERROR [io.undertow.request] (default task-13) UT005023: Exception handling request to /updateUserAnswer/11: org.lightadmin.core.view.exceptions.BadRequestException: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Parameter value [11] did not match expected type [java.lang.Integer (n/a)]

这是我的 Controller

@RequestMapping(value = "/updateUserAnswers/{userId}", method = RequestMethod.PUT)
public Response updateUserAnswer(@PathVariable("userId") Long userId,
@RequestBody @Valid UserAnswer userAnswer) {

userAnswerService.updateUserAnswer(userId, userAnswer);

HashMap<String, Object> response = new HashMap<>();
response.put("messages", null);
response.put("success", Boolean.valueOf(true));
return Response.instance().friendlyName("user-answer-updated").object(response).statusCode(HttpStatus.OK);
}

这是我的服务:

public void updateUserAnswer(Long userId,UserAnswer userAnswer) {

List<UserAnswer> currentUserAnswer = userAnswerRepo.getUserAnswers(userId, userAnswer.getAnswerId().longValue(),null);
currentUserAnswer.get(0).setLabel(userAnswer.getLabel());

List<UserAnswerField> fieldsList = new ArrayList<UserAnswerField>();


for (UserAnswerField userAnswerField : userAnswer.getAnswerFields()) {
UserAnswerField currentUserAnswerField = userAnswerFieldRepo.getUserAnswerField(userAnswerField.getfieldId());
if (currentUserAnswerField != null) {
currentUserAnswerField.setfield(userAnswerField.getfield());
currentUserAnswerField.setTypeId(userAnswerField.getTypeId());
fieldsList.add(currentUserAnswerField);
//userAnswerFieldRepo.save(currentUserAnswerField);
}
}
currentUserAnswer.get(0).setAnswerFields(fieldsList);

userAnswerRepo.save(currentUserAnswer.get(0));
}

存储库:

public List<UserAnswer> getUserAnswers(Long userId,Long answerId ,String pageType) {
try {
String qlString = ""
+ " Select sf from UserAnswer sf "
+ " where "
+ " sf.userId = :userId ";
if(pageType != null){
qlString+= " and sf.type=:type ";
}
if(answerId != null){
qlString+= " and sf.answerId=:answerId ";
}
qlString+=" order by sf.insertionDate";

Query query = getEntityManagerFactory().createQuery(qlString);
query.setParameter("userId", userId);
if(pageType != null){
query.setParameter("type", pageType);
}
if(answerId != null){
query.setParameter("answerId", answerId);
}
return (List<UserAnswer>) query.getResultList();
} catch (javax.persistence.NoResultException e) {
return null;
}
}

你能帮帮我吗?提前致谢

最佳答案

错误发生在 query.setParameter("userId", userId);,因为当 hqlQuery 需要 Integer 时,您试图设置 Long。

解决方案:

  • 将类型 Long -> Integer 从 controller 更改为 Repository

  • 使用 .intValue()(与其说是解决方案,不如说是快速修复)

  • 将实体中的 ID 更改为 Long 类型。

关于java - 不匹配预期类型 spring 框架错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31533519/

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