Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
So, I'm trying to create comments on a post using spring mvc, spring boot, spring data, jpa, and thymeleaf, and so far I can get to the specific page I want, using the controller and pathvariables, and I can load up the page just how I want, but when I go to submit the comment I get the error
There was an unexpected error (type=Bad Request, status=400).
Failed to convert value of type 'java.lang.String' to required type 'com.example.domain.Comment'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type java.lang.Long for value 'comment 1'; nested exception is java.lang.NumberFormatException: For input string: "comment1"
This error is only in my browser, nothing comes up in the console in my IDE. Also I can access the page just fine, so there I don't think there's an issue in my get method in my controller, but I'm not really sure where the problem is, so I'll show you guys some of my code.
Here's my controller.
private PostRepository postRepo;
@RequestMapping(value="viewCourse/post/{postId}", method=RequestMethod.GET)
public String postViewGet (@PathVariable Long postId, ModelMap model)
Post post = postRepo.findOne(postId);
model.put("post", post);
Comment comment = new Comment();
model.put("comment", comment);
return "post";
@RequestMapping(value="viewCourse/post/{postId}", method=RequestMethod.POST)
public String postViewPost (@ModelAttribute Comment comment, @PathVariable Long postId, ModelMap model)
Post post = postRepo.findOne(postId);
comment.setPost(post);
post.getComments().add(comment);
postRepo.save(post);
return "redirect:/viewCourse/{postId}";
@Autowired
public void setPostRepo(PostRepository postRepo) {
this.postRepo = postRepo;
Here's my thymeleaf html page
<div class="PostContent">
<h2 th:text = "${post.title}"></h2>
<p th:text = "${post.content}"></p>
<div class="CommentPost">
<form th:action="${post.id}" method="post" th:object="${comment}" id="comment">
<div class="form-group">
<textarea rows="2" th:field="${comment.comment}" class="form-control" placeholder="comment" id="comment"></textarea>
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
<input type="submit" value="Comment" class="btn btn-success"/>
</form>
<div class="Comments">
<div th:each = "comment : ${comments}" th:object="${comment}">
<span th:text="${comment.comment}"></span>
<div th:if = "${#lists.isEmpty(comments)}">
There are no comments to display
Also on this page the message comes up "There are no comments to display", just like I tell it to in the code, but it still says "There are no comments to display" even if I manually insert a comment into the database.
Here's my comment object, although I'm pretty sure that's fine.
@Entity
public class Comment {
public Long id;
public String comment;
public Post post;
public User user;
@GeneratedValue
public Long getId() {
return id;
public void setId(Long id) {
this.id = id;
public String getComment() {
return comment;
public void setComment(String comment) {
this.comment = comment;
@ManyToOne
public Post getPost() {
return post;
public void setPost(Post post) {
this.post = post;
@ManyToOne
public User getUser() {
return user;
public void setUser(User user) {
this.user = user;
And my postRepo, although this should be fine, just thought I'd include it
public interface PostRepository extends JpaRepository <Post, Long>{
If anyone can see my issue, and let me know, that would be awesome, thanks.
When you use th:object don't have to reference to object, you access directly atributes of object. Try with this code:
<div class="PostContent">
<h2 th:text = "${post.title}"></h2>
<p th:text = "${post.content}"></p>
<div class="CommentPost">
<form th:action="${post.id}" method="post" th:object="${comment}" id="comment">
<div class="form-group">
<textarea rows="2" th:field="*{comment}" class="form-control" placeholder="comment" id="comment"></textarea>
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
<input type="submit" value="Comment" class="btn btn-success"/>
</form>
I don't see in the controller where you put the comments in the model. I suppose that comments there are inside the post so modify the refereces of comments to post.comments
<div th:each = "comment : ${post.comments}" th:object="${comment}">
<span th:text="*{comment}"></span>
<div th:if = "${#lists.isEmpty(post.comments)}">
There are no comments to display
The problem is that the name of Class - Comment - and the field - comment - are the same, regarding to insensitive way, causing problem due to Java Reflection use to read the field and its class.
The solution was to rename the field, like "comment" to "commentary", and to avoid to change again in database, if there is some, just put the annotation @Column(name="comment") above the field.
Perhaps, reference from main template on thymeleaf template in question look like:
th:href="@{/post/{${post.getId()}}",
but it should look like:
th:href="@{/post/{postId}(postId=${post.getId()})}"
In my occasion, it helped me
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.