Witam,
Robię aplikację na zaliczenie i nie mogę sobie poradzić z przesłaniem obiektu z formularza do kontrolera.
Kontroler:
@RequestMapping("/home/profile")
@Controller
public class ProfileController {
private static Logger logger = LoggerFactory.getLogger(ProfileController.class);
private final UsersRepository usersRepository;
private final InvitationsService invitationsService;
public ProfileController(UsersRepository usersRepository, InvitationsService invitationsService){
this.usersRepository = usersRepository;
this.invitationsService = invitationsService;
}
@RequestMapping(value = "/{userId}", method = RequestMethod.GET)
public ModelAndView profile(@PathVariable("userId") Long userId, Model model) throws Exception {
Optional<User> oUser = this.usersRepository.findByUserId(userId);
logger.info("USERID {}", userId);
logger.info("USER {}", oUser.get().getEmail());
oUser.orElseThrow(() -> new UserNotExistsException());
User user = oUser.get();
User logged = ((CustomUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser();
ModelAndView mv = new ModelAndView("profile");
mv.addObject("user", user);
mv.addObject("friends", this.invitationsService.usersAreFriends(user, logged));
mv.addObject("invitation", new Invitation());
return mv;
}
@RequestMapping(value = "/{userId}", method = RequestMethod.POST)
public ModelAndView invite(@PathVariable("userId") Long userId,
@ModelAttribute("invitation")Invitation invitation,
BindingResult result,
Model model) throws Exception {
if (result.hasErrors()) {
for (ObjectError error: result.getAllErrors()) {
logger.warn("ERROR {}", error.getDefaultMessage());
}
}
User user = invitation.getFromUser();
logger.info("FROM USER: " + user.getUserId());
this.invitationsService.addNewUser(invitation);
return profile(userId, model);
}
}
JSP:
<form:form method="post" modelAttribute="invitation">
<form:input path="fromUser" type="hidden" name="fromUser" value="${logged}"/>
<form:input path="toUser" type="hidden" name="toUser" value="${user}"/>
<form:button id="sendInvitation" type="submit" class="btn btn-primary btn-lg"><spring:message code="profile.addToFriends" /></form:button>
</form:form>
Błąd jaki leci to:
2018-03-14 19:10:14.610 WARN 9256 --- [nio-8080-exec-5] c.u.S.controller.ProfileController : ERROR Failed to convert property value of type 'java.lang.String' to required type 'com.ujazdowski.SocialPortal.model.tables.User' for property 'fromUser'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'User(userId=2, roles=[Role(userRoleId=1, role=USER)], firstName=Bartosz, secondName=Ujazdowski, [email protected], password=$2a$10$da7l/9VRpIPBm1z0LiONwO7wRXMs55.5QOkdpFVQ3/eqO9FBu4IXu, lastTimeOnline=null, profilePhotoId=null, male=true)'; nested exception is java.lang.NumberFormatException: For input string: "User(userId=2,roles=[Role(userRoleId=1,role=USER)],firstName=Bartosz,secondName=Ujazdowski,[email protected],password=$2a$10$da7l/9VRpIPBm1z0LiONwO7wRXMs55.5QOkdpFVQ3/eqO9FBu4IXu,lastTimeOnline=null,profilePhotoId=null,male=true)"
2018-03-14 19:10:14.611 WARN 9256 --- [nio-8080-exec-5] c.u.S.controller.ProfileController : ERROR Failed to convert property value of type 'java.lang.String' to required type 'com.ujazdowski.SocialPortal.model.tables.User' for property 'toUser'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'User(userId=4, roles=[Role(userRoleId=1, role=USER)], firstName=Kamil, secondName=Stonoga, [email protected], password=$2a$10$eB26PJHiLWOjS7jf/V2bK.5jJZPMUEVViyjBPirN83c7W6/3Hdq8G, lastTimeOnline=null, profilePhotoId=null, male=true)'; nested exception is java.lang.NumberFormatException: For input string: "User(userId=4,roles=[Role(userRoleId=1,role=USER)],firstName=Kamil,secondName=Stonoga,[email protected],password=$2a$10$eB26PJHiLWOjS7jf/V2bK.5jJZPMUEVViyjBPirN83c7W6/3Hdq8G,lastTimeOnline=null,profilePhotoId=null,male=true)"
2018-03-14 19:10:14.613 ERROR 9256 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at com.ujazdowski.SocialPortal.controller.ProfileController.invite(ProfileController.java:68) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_162]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_162]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_162]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_162]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
Z góry dziękuje za pomoc ;)