Failed to convert value from property of type 'java.lang.String'

Asked

Viewed 263 times

0

I am trying to submit a form that in one of the classes there is a relationship, I would like to know how to do this, because every time I try to submit I get the error of "Failed to Convert Property value of type 'java.lang.String'"

Below is my model

@Entity
public class Musica{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotNull(message = "Campo está vazio ou nulo.")
    @Size(min = 2, max = 50)
    @Column(nullable = false, name = "titulo", length = 50)
    private String titulo;

    @Enumerated(EnumType.STRING)
    private TipoMusica generoMusica;


        @ManyToOne
        @JoinColumn(name = "id_playlist_fk")
        private Playlist playlist;

        @ManyToOne
        @JoinColumn(name = "id_artista_fk")
        private Artista artista;

My form in the view:

<form class="form-horizontal" action="/musicas/Adicionar-Musica" th:object="${musica}" method="post">
			
				<div class="row">
				<div class="form-group col-md-4 offset-md-4">
					<div class="alert alert-danger" role="alert" th:if="${#fields.hasErrors('titulo')}">
						<label class="validation-message" th:errors="*{titulo}"></label>
					</div>
						<label class="lbl">Nome da Música</label>
						<input type="text" class="form-control" th:field="*{titulo}" placeholder="Ex: Por baixo ou Por Cima de Mim..">
					</div>
				</div>
				
				
				<div class="row">
					<div class="form-group col-md-4 offset-md-4">
					<div class="alert alert-danger" role="alert" th:if="${#fields.hasErrors('artista')}">
						<label class="validation-message" th:errors="*{artista}"></label>
					</div>
						<label class="lbl" >Artista/Banda</label>
						<input type="text" class="form-control" th:field="*{artista}" placeholder="Ex: Henrique e Juliano">
				</div>
			</div>
				
<a class="btn btn-primary mr-2 mt-2" href="/"><i class="fas fa-angle-left"></i>&nbsp;VOLTAR</a>
			<button type="submit" class="btn btn-success mt-2"><i class="fas fa-save"></i>&nbsp;SALVAR INFORMAÇÕES</button>				
			</form>

Finally my controller:

@PostMapping("/Adicionar-Musica")
public ModelAndView inserirMusica(@Valid Musica musica, BindingResult br) {
    ModelAndView mv = new ModelAndView();
    if(br.hasErrors()) {
        mv.setViewName("musicas/adicionar-musica");
        mv.addObject("musica");
    }else {
        mv.setViewName("redirect:/musicas/lista-musicas");
        repositoriomusica.save(musica);
    }
    return mv;

}

The error that comes to me when trying to save the information:

Failed to convert property value of type java.lang.String to required type br.com.neo.springmusic.model.Artista for property artista; nested exception is java.lang.IllegalStateException: Cannot convert value of type java.lang.String to required type br.com.neo.springmusic.model.Artista for property artista: no matching editors or conversion strategy found

I would like to know how to save an artist without getting the conversion error?

  • Put the Stacktrace of error, pf.

  • ready put the return that appears when trying to save

2 answers

0

The error is telling you that you cannot convert a string to a complex object of the artist type.

On your form you should be passing something like artista.nome imagine.

Check that the object will be instantiated correctly in the submission.

0

Guys I managed to solve the problem! I made some modifications as I would fix the errors that were appearing. But I’d like to thank you for the "artist.name" tip. This solved however I received another error because I had not created the getters and setters of the class but I managed to solve and submit the form. I thank you and stay as a tip for those who present this same problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.