O
O
Orkhan Hasanli2018-12-08 21:13:29
Java
Orkhan Hasanli, 2018-12-08 21:13:29

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>

There is a controller to handle the request:
@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";
}

I get an exception when submitting the form:
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"

Here are the POST request parameters:
5c0c09598e4e3716135379.jpeg
revision entity
@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;
}


Why is it trying to convert the String type to Long ? All other forms of the site work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2018-12-09
@azerphoenix

The solution turned out to be quite simple:
- It was necessary to remove Revision revisionNew
- create a new revision in the method body and save

Revision revisionNew = new Revision(revision);
revisionService.addRevision(revisionNew);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question