'Failed to convert value of type 'org.springframework.validation.BeanPropertyBindingResult' to required type 'java.lang.String';

I wanted to add some validation on my bean, I tried to do that, I got everything perfect but I'm not getting error message which I had set in the bean file. Even I used addFlashAttribute but I got error after submit the form. I'm using here thymeleaf too. I had tried all methods which are on StackOverflow but I got error only. help me any one....

pom.xml

<dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>>
    </dependency>

<!-- hibernate-validator -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.1.3.Final</version>
</dependency>

Category.java

@Entity
@DynamicInsert
@DynamicUpdate
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = {"categoryName"})})
public class Category {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long cId;

    @NotBlank(message = "Field is empty !!")
    @NotEmpty(message = "Field is empty !!")
    @NotNull
    @Size(min = 2, message = "Min character should be atleast 2 !!")
    private String categoryName;

AdminController.java

 @GetMapping("/category/{page}")
    public String addCategory(@PathVariable("page") Integer page,
            @RequestParam(required = false, name = "categoryMess") String categoryMess,
            @RequestParam(required = false, name = "catDataname") String catDataname,
            @RequestParam(required = false, name = "catcardtitle") String catcardtitle,
            Model model) {


                if (!model.containsAttribute("category")) {
                    model.addAttribute("category", new Category());
                }

        // if (!model.asMap().containsKey("category")) {
        //     model.addAttribute("category", new Category());
        // }
        //didn't work

       
        // pagination has started !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        Pageable pageable = PageRequest.of(page, ConstantData.PAGE_SIZE);
        Page<Category> pagecategoryDataList = categoryDaoImpl.gettingCategoryData(pageable);
        // storing all ategory data in list from database
        List<Category> categoryDataList = pagecategoryDataList.getContent();
        // pagination has ended!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        return "adminSpace/addCategory";
    }

CategoryController.java

@PostMapping("/saveCategory/{currentPage}")
    public ModelAndView savingCategory(@Valid @ModelAttribute Category category, BindingResult result, ModelMap model, RedirectAttributes rediret) {


        if(result.hasErrors()){rediret.addFlashAttribute("org.springframework.validation.BindingResult.category", result);
            rediret.addFlashAttribute("category", category);
            return new ModelAndView("redirect:/admin/category/" + currentPage, model);
        }

        return new ModelAndView("redirect:/admin/category/" + currentPage, model);
    }

addCategory.html

 <form th:action="@{/admin/category/saveCategory}+'/'+${currentPage}" th:object="${category}" method="post">
                                    <div class="form-group">
                                        <!-- th:value="categoryName" -->
                                        <input class="form-control" th:value="${catDataname}"  th:classappend="${#fields.hasErrors('categoryName')?'is-invalid':''}"
                                            type="text" placeholder="Category name" name="categoryName" th:field="*{categoryName}">
                                    </div>
                                    <div class="alert alert-danger" th:each="e : ${#fields.errors('categoryName')}" th:text="${e}"></div>
                                    
                                    <!-- <div class="alert alert-warning" th:if="${#fields.hasErrors('categoryName')}" th:errors="*{categoryName}"></div> -->

                                    <div class="form-group text-right">
                                        <button type="submit" class="btn btn-success px-4 mt-2">save</button>
                                    </div>
                                </form>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source