multiple select - different behavior (from database and resttemplate)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

multiple select - different behavior (from database and resttemplate)

moshell
Hello, everybody.
I'm newbie in thymeleaf.
I have a form for creating a user. (a part of "registration.html")

                        <select class="form-control input-sm" id="userProfiles" name="userProfiles" multiple="multiple" th:required="true">
                            <option th:each="role : ${allRoles}"
                                    th:value="${{role}}"
                                    th:text="${role.type}"
                                    th:selected="${role.id == 1}"
                            >Role
                            </option>
                        </select>

th:selected is used for preselecting default role

with this controller it works fine (via repositories)
    @RequestMapping(value = { "/newUser" }, method = RequestMethod.GET)
    public String newUser(ModelMap model) {
        User user = new User();
        List<UserProfile> roles = userProfileRepository.findAll();

        model.addAttribute("user", user);
        model.addAttribute("allRoles", roles);
        return "registration";
    }

    @RequestMapping(value = { "/newUser" }, method = RequestMethod.POST)
    public String saveUser(@Valid User user, BindingResult result,
                           ModelMap model) {

        if (result.hasErrors()) {
            model.addAttribute("allRoles", userProfileRepository.findAll());
            model.addAttribute("user", user);
            return "registration";
        }
        userRepository.save(user);
        return "registrationsuccess";
    }

But, when i use RestTemplate for the same operation it thows exceptions

    @RequestMapping(value = { "/newUser" }, method = RequestMethod.GET)
    public String newUser(ModelMap model) {
        User user = new User();
        RestTemplate restTemplate = new RestTemplate();
        List<UserProfile> userProfiles = Arrays.asList(restTemplate.getForObject("http://localhost:8080/api/roles", UserProfile[].class));
        ArrayList<UserProfile> roles = new ArrayList<>(userProfiles);

        model.addAttribute("user", user);
        model.addAttribute("allRoles", roles);
        return "registration";
    }

    @RequestMapping(value = { "/newUser" }, method = RequestMethod.POST)
    public String saveUser(@Valid User user, BindingResult result,
                           ModelMap model) {

        if (result.hasErrors()) {
            RestTemplate restTemplate = new RestTemplate();
            List<UserProfile> userProfiles = Arrays.asList(restTemplate.getForObject("http://localhost:8080/api/roles", UserProfile[].class));
            ArrayList<UserProfile> roles = new ArrayList<>(userProfiles);

            model.addAttribute("allRoles", roles);
            model.addAttribute("user", user);
            return "registration";
        }
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.postForObject("http://localhost:8080/api/users", user, User.class);

        return "registrationsuccess";
    }

There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: "class path resource [templates/registration.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/registration.html]")
        at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241)
        at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100)
        at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:666)
        at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098)
        at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072)
        at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362)
        at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189)
        at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1371)
        at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1117)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1056)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:748)
Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "role" (template: "registration" - line 60, col 37)
        at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393)
        at org.attoparser.MarkupParser.parse(MarkupParser.java:257)
        at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230)
        ... 52 more
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "role" (template: "registration" - line 60, col 37)
        at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:290)
        at org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166)
        at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66)
        at org.thymeleaf.standard.expression.Expression.execute(Expression.java:109)
        at org.thymeleaf.standard.expression.Expression.execute(Expression.java:138)
        at org.thymeleaf.standard.processor.AbstractStandardExpressionAttributeTagProcessor.doProcess(AbstractStandardExpressionAttributeTagProcessor.java:144)
        at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74)
        at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95)
        at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633)
        at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1314)
        at org.thymeleaf.engine.OpenElementTag.beHandled(OpenElementTag.java:205)
        at org.thymeleaf.engine.Model.process(Model.java:282)
        at org.thymeleaf.engine.Model.process(Model.java:290)
        at org.thymeleaf.engine.IteratedGatheringModelProcessable.processIterationModel(IteratedGatheringModelProcessable.java:367)
        at org.thymeleaf.engine.IteratedGatheringModelProcessable.process(IteratedGatheringModelProcessable.java:221)
        at org.thymeleaf.engine.ProcessorTemplateHandler.handleCloseElement(ProcessorTemplateHandler.java:1640)
        at org.thymeleaf.engine.TemplateHandlerAdapterMarkupHandler.handleCloseElementEnd(TemplateHandlerAdapterMarkupHandler.java:388)
        at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler$InlineMarkupAdapterPreProcessorHandler.handleCloseElementEnd(InlinedOutputExpressionMarkupHandler.java:322)
        at org.thymeleaf.standard.inline.OutputExpressionInlinePreProcessorHandler.handleCloseElementEnd(OutputExpressionInlinePreProcessorHandler.java:220)
        at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler.handleCloseElementEnd(InlinedOutputExpressionMarkupHandler.java:164)
        at org.attoparser.HtmlElement.handleCloseElementEnd(HtmlElement.java:169)
        at org.attoparser.HtmlMarkupHandler.handleCloseElementEnd(HtmlMarkupHandler.java:412)
        at org.attoparser.MarkupEventProcessorHandler.handleCloseElementEnd(MarkupEventProcessorHandler.java:473)
        at org.attoparser.ParsingElementMarkupUtil.parseCloseElement(ParsingElementMarkupUtil.java:201)
        at org.attoparser.MarkupParser.parseBuffer(MarkupParser.java:725)
        at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:301)
        ... 54 more
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1001E: Type conversion problem, cannot convert from com.naranov.rest_client.model.UserProfile to java.lang.String
        at org.springframework.expression.spel.support.StandardTypeConverter.convertValue(StandardTypeConverter.java:75)
        at org.springframework.expression.common.ExpressionUtils.convertTypedValue(ExpressionUtils.java:57)
        at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:367)
        at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:277)
        ... 79 more
Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [com.naranov.rest_client.model.UserProfile] to type [java.lang.String]
        at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321)
        at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:194)
        at org.springframework.expression.spel.support.StandardTypeConverter.convertValue(StandardTypeConverter.java:70)
        ... 82 more




PS. i've tested restcontroller within PostMan. it works fine.