Answer the question
In order to leave comments, you need to log in
Spring - Why does a ConversionFailedException occur?
Hello!
I ran into a rather strange problem while working with Spring.
I use Spring Boot, Spring Security, Thymeleaf and etc.
There is a form displayed in the modal:
<form id="revisions" th:action="@{'/posts'}" method="post">
<textarea name="revision" class="form-control" rows="5" id="revision" placeholder="Ваш комментарий ..." required></textarea>
<button type="submit" th:formaction="@{'/posts/revision/'}" formmethod="post" id="revisionsOwnerAdd" sec:authorize="hasAnyAuthority('ADMIN','OWNER')" class="btn btn-warning float-right">Отправить на доработку</button>
<button type="submit" th:formaction="@{'/posts/checking/'}" formmethod="post" id="revisionsAuthorAdd" sec:authorize="hasAuthority('AUTHOR')" class="btn btn-warning float-right">Отправить на проверку</button>
</form>
@PreAuthorize("hasAuthority('AUTHOR')")
@PostMapping(value = "/checking/{id}")
public String checkingPost(
@PathVariable(value = "id") Long id,
// Проблема наблюдается здесь с RequestParam String Revision
@RequestParam("revision") String revision,
Revision revisionNew
){
Optional<Post> post = postService.getPost(id);
if(post.isPresent()){
post.orElse(null).setPostStatus(PostStatus.CHECKING);
revisionNew.setRevContent(revision);
revisionNew.setRevCreationDate(LocalDate.now());
revisionNew.setPost(post.orElse(null));
revisionService.addRevision(revisionNew);
}
postService.updatePost(id, post.orElse(null));
return "redirect:/posts?checking";
}
Failed to convert value of type 'java.lang.String' to required type 'info.md7.wpat.models.Revision'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'sfdfsf'; nested exception is java.lang.NumberFormatException: For input string: "sfdfsf"
@Entity @Data
public class Revision {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long revId;
@Lob
@Column( length = 5000 )
private String revContent;
private LocalDate revCreationDate;
// Constructor
private Revision(){}
@ManyToOne
@JoinColumn(name = "postId")
private Post post;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question