I have researched question and couldn't find appropriate answer.
I am trying to return only certain columns from table using custom query with Spring Data JPA in my Spring Rest application. However query always throws exception when executed.
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [org.forum.api.model.Message]
I know that it is possible to use String but why are Message objects not Serialized properly into JSON even thought I have created model for it in a sub-package of Spring Boot main ?
Here is my model class.
@Entity
@Table(name="message")
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "text_id")
private long id;
@NotNull
private String author;
@NotNull
private String text;
@NotNull
private String recepient;
public long getId() {return id;}
public void setId(long id) {this.id = id;}
public String getAuthor() {return author;}
public void setAuthor(String author) {this.author = author;}
public String getText() {return text;}
public void setText(String text) {this.text = text;}
public String getRecepient() {return recepient;}
public void setRecepient(String recepient) {this.recepient = recepient;}
}
Here is controller class.
@RestController
@RequestMapping("/api")
public class MessageController {
@Autowired
private MessageService messageService;
@GetMapping("/message/{id}")
public Message getMessageTextById(@PathVariable(value="id") Long id) {
return messageService.getMessageTextById(id);
}
}
Here is service class.
@Service
public class MessageServiceImpl implements MessageService {
@Autowired
MessageRepository messageRepo;
@Override
public Message getMessageTextById(Long id) {
return messageRepo.findMessageTextById(id);
}
}
Here is Repository Class
@Repository
public interface MessageRepository extends JpaRepository<Message, Long> {
@Query("SELECT m.author, m.text FROM Message m WHERE m.id = :id")
Message findMessageTextById(@Param("id") Long id);
}