我有兩個數據模型的用戶和車:未能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(); –
我剛試過你的例子,我遇到了同樣的問題! –
你能提供完整的錯誤堆棧跟蹤嗎?你確定它在轉換器類中錯誤嗎? –