Search code examples
javahtmlspringthymeleaf

Spring/Thymeleaf - Failed to convert value of type 'java.lang.String' to required type


I am new to Spring and Thymeleaf, and I do not know what went wrong here. When submitting my form, I get THIS error:

There was an unexpected error (type=Bad Request, status=400). Failed to convert value of type 'java.lang.String' to required type 'br.com.teste.segware.domain.post.Post'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Integer] for value 'Some text...'; nested exception is java.lang.NumberFormatException: For input string: "Sometext..." org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'br.com.teste.segware.domain.post.Post'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Integer] for value 'Some text...'; nested exception is java.lang.NumberFormatException: For input string: "Sometext..."

Here is my Post class: (I use Lombok, so the getters and setters are self-generated)

@Getter
@Setter
@Entity
@Table(name = "post")
public class Post implements Serializable {

    @EqualsAndHashCode.Include
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    private Integer id;

    @NotBlank
    @Column(nullable = false)
    private String nome;

    @NotBlank
    @Size(max = 800)
    @Column(nullable = false)
    private String post;
}

My Controller:

@Controller
public class IndexController {

    @Autowired
    private PostService postService;

    @PostMapping("/savePost")
    String savePost(@ModelAttribute("post") Post post) {
        postService.savePost(post);
    
        return "redirect:/";
   }
}

And my HTML form:

<form method="post" th:object="${post}" th:action="@{/savePost}">
            <fieldset>
                <input type="hidden" th:field="*{id}" />
            
                <label for="name">Nome:</label> <br/>
                <input type="text" id="name" name="name" th:field="*{nome}" placeholder="Nome..." /> <br/><br/>
                
                <label for="post">O que você gostaria de dizer?</label> <br/>
                <textarea id="post" name="post" th:field="*{post}" ></textarea> <br/><br/>
                <input type="submit" value="Postar" />
            </fieldset>
</form>

Why is this thing trying to convert the <textarea> from String to some number with NumberFormat stuff ?

The Entity named Post clearly declares the field post as a String. So why does Spring thinks it is some kind of Number when submitting? Obviously, when I put some numbers in textarea, it saves to the database. But I need String to be saved...

Someone please enlighten me.

Thanks in advance!

EDIT

Here is the Repository AND Service Class, just to be sure.

Service...

@Service
public class PostService {

    @Autowired
    private PostRepository postRepository;

    public void savePost(Post post) {
        postRepository.save(post);
    }
}

Repo...

public interface PostRepository extends JpaRepository<Post, Integer> {

}

Solution

  • There seems to be a name collision since both the object name and the variable name are same (in your case, post).

    Either change the column name in the entity class to other than post and then change in the html. Or, change the backing object name in the controller and html to something other than post. Both are working for me.