您好我是WebServices的新手,在我的My-Sql数据库中我有学生表,其中包含一些列为“ user_id ”和“名称< / strong>“和”标记“
我想基于userId更新一行,因为我写了下面这段代码,但我得到的例外情况如下所示,有些人可以帮助我
Controller [com.ensis.sample.controller.SampleController]
Method [public com.ensis.sample.model.StatusObject com.ensis.sample.controller.SampleController.updateStudentListById(int)]
org.springframework.web.bind.MissingServletRequestParameterException: Required int parameter 'userId' is not present
@RequestMapping(value="/update",method=RequestMethod.POST,produces={"application/json"})
@ResponseBody
public StatusObject updateStudentListById(@RequestParam int userId){
return userService.updateStudentDetailsById(userId);
}
@Transactional
public StatusObject updateStudentDetailsById(int id){
Users users = usersdao.updateStudentDetailsById(id);
if(users!=null){
users.setName("Sample");
users.setMarks(99.99);
}
StatusObject statusObject = new StatusObject();
boolean status = usersdao.updateUser(users);
if(status==true){
statusObject.setStatus(false);
statusObject.setMessage("Success");
return statusObject;
}else{
statusObject.setStatus(true);
statusObject.setMessage("Failure");
return statusObject;
}
}
public Users updateStudentDetailsById(int userId){
System.out.println("UserId is=====>"+userId);
String hql = "FROM Users s WHERE " + "s.user_id = :userId";
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery(hql);
query.setParameter("user_id", userId);
List<?>list = query.list();
Iterator<?>itr = list.iterator();
if(itr.hasNext()){
Users users = (Users)itr.next();
return users;
}
session.flush();
session.clear();
return null;
}
@Entity
@Table(name = "student")
public class Users {
@Id
private int user_id;
private String name;
private int rank;
private double marks;
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public double getMarks() {
return marks;
}
public void setMarks(double marks) {
this.marks = marks;
}
答案 0 :(得分:1)
@Krish,当你发帖时,你通常会使用Spring @RequestBody
,如下所示:
@RequestMapping(value="/update",method=RequestMethod.POST,produces={"application/json"})
@ResponseBody
public StatusObject updateStudentListById(@RequestBody User user){
return userService.updateStudentDetailsById(userId);
}
您需要将JSON对象传递给此控制器方法。 Spring将为您反序列化JSON。
当您说@RequestParam
时,它希望找到请求参数,例如
/update?userId=1
PS :仅发送ID来更新资源不是一种好习惯。
答案 1 :(得分:0)
您是否将它用作RestController。例外来自控制器,因为它需要来自客户端的参数。请验证您是否在pathParam中传递userID。