我有一个基于spring框架的服务 我想知道为什么我有这个例外,虽然我发送一个整数到我的服务
错误[io.undertow.request](默认任务-13)UT005023:异常 处理/ updateUserAnswer / 11的请求: org.lightadmin.core.view.exceptions.BadRequestException: org.springframework.web.util.NestedServletException:Request 处理失败;嵌套异常是 java.lang.IllegalArgumentException:参数值[11]不匹配 期望的类型[java.lang.Integer(n / a)]
这是我的控制器
@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;
}
}
你可以帮帮我吗?提前谢谢
答案 0 :(得分:1)
错误发生在query.setParameter("userId", userId);
,因为当hqlQuery期待一个Integer时你正试图设置一个Long。
解决方案:
更改类型Long
- &gt; Integer
从控制器到存储库
使用.intValue()
(更多快速解决方案而不是解决方案)
将实体中的ID更改为Long类型。
答案 1 :(得分:0)
您会收到异常,因为您需要绑定基本类型。
试试这个:
public Response updateUserAnswer(@PathVariable("userId") long userId,
@RequestBody @Valid UserAnswer userAnswer) { [...] }