嗨,我是新来的Web服务,在我的My-SQL数据库中,我有 student 带有某些列的表,这些都是" user_id "one_answers" 名称"one_answers" 标记"
我想根据我在下面写的代码来更新一行,但是我得到的例外,如下,我可以帮助我
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;
}
@krish,当您发布某些内容时,通常使用Spring的@RequestBody
,如下所示:
@RequestMapping(value="/update",method=RequestMethod.POST,produces={"application/json"})
@ResponseBody
public StatusObject updateStudentListById(@RequestBody User user){
return userService.updateStudentDetailsById(userId);
}
您需要将JSON对象传递到此控制器方法。春天将为您挑战JSON。
当您说@RequestParam
时,它希望找到请求参数 /update?userId=1
PS :仅发送ID来更新资源不是一个好练习。
您是否将其用作RESTCONTROLLER。该切除来自控制器,因为它希望从客户端出现参数。请验证是否在PathParam中传递了UserID。