我有两个数据模型的用户和车:未能Object类型的值转换为所需类型的对象春
User.java:
@Entity
@Table(name="APP_USER")
public class User implements Serializable{
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
......
@OneToMany(mappedBy="user",cascade=CascadeType.ALL)
private Set<Car> cars = new HashSet<Car>();
Car.java:
@Entity
public class Car implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id ;
.....
@ManyToOne(optional=false)
@JoinColumn(name="user_fk")
private User user;
在控制器中,我想添加一个新用户,所以
AppController.java:
@Controller
@RequestMapping("/")
@SessionAttributes("roles")
public class AppController {
@RequestMapping(value = { "/newuser" }, method = RequestMethod.GET)
public String newUser(ModelMap model) {
User user = new User();
model.addAttribute("user", user);
model.addAttribute("edit", false);
model.addAttribute("loggedinuser", getPrincipal());
return "registration";
}
@RequestMapping(value = { "/newuser" }, method = RequestMethod.POST)
public String saveUser(@ModelAttribute @Valid User user, BindingResult result,
ModelMap model) {
if (result.hasErrors()) {
return "registration";
}
if(!userService.isUserSSOUnique(user.getId(), user.getSsoId())){
FieldError ssoError =new FieldError("user","ssoId",messageSource.getMessage("non.unique.ssoId", new String[]{user.getSsoId()}, Locale.getDefault()));
result.addError(ssoError);
return "registration";
}
userService.saveUser(user);
model.addAttribute("success", "User " + user.getFirstName() + " "+ user.getLastName() + " registered successfully");
model.addAttribute("loggedinuser", getPrincipal());
return "registrationsuccess";
}
另外,我创建了一个名为StringToUser类(实现转换器,所以我可以添加包含用户新车)
StringtoUser.java:
@Autowired
UserService userService ;
@Override
public User convert(Object element) {
Integer id = Integer.parseInt((String)element);
User user = userService.findById(id);
return user;
}
在我添加StringToUser类之前,AppController.java和saveUser方法正常工作。但是在创建d StringToUser类,我得到了saveUser方法错误
The error is : WARNING: Failed to bind request element: org.springframework.beans.TypeMismatchException: Failed to convert value of type [com.websystique.springmvc.model.User] to required type [com.websystique.springmvc.model.User]; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [com.websystique.springmvc.model.User] to type [@org.springframework.web.bind.annotation.ModelAttribute @javax.validation.Valid com.websystique.springmvc.model.User] for value 'User [id=null, ssoId=alaa, password=alaa1991, firstName=, lastName=, email=, userProfiles=null, accounts=null, userDocuments=[], cars=[], documents=[]]'; nested exception is java.lang.ClassCastException: com.websystique.springmvc.model.User cannot be cast to java.lang.String
编辑:
错误:
WARNING: Failed to bind request element: org.springframework.beans.TypeMismatchException: Failed to convert value of type [com.websystique.springmvc.model.User] to required type [com.websystique.springmvc.model.User]; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [com.websystique.springmvc.model.User] to type [@org.springframework.web.bind.annotation.ModelAttribute @javax.validation.Valid com.websystique.springmvc.model.User] for value 'User [id=null, ssoId=alaa, password=alaa1991, firstName=, lastName=, email=, userProfiles=null, accounts=null, userDocuments=[], cars=[], documents=[]]'; nested exception is java.lang.NullPointerException
看来您的convert类正在接收对User类的引用,而不是您需要的String表示。你试过替换:Integer id = Integer.parseInt((String)element); for:Integer id =((User)element).getId(); –
我刚试过你的例子,我遇到了同样的问题! –
你能提供完整的错误堆栈跟踪吗?你确定它在转换器类中错误吗? –