0

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);

}

1 Answer 1

4

If you want to retrieve only certain columns you can use a simple bean class:

public class CustomMessage{
  private String author;
  private String text;

  public CustomMessage(String author, String text) {
    this.author = author;
    this.author = text;
  }
}

Then return a bean instance from your repository:

@Query("SELECT new path_to_class.CustomMessage(m.author, m.text) FROM Message m WHERE m.id = :id")

Or retrieve a map:

 @Query("SELECT new map(m.author as author, m.text as text) FROM Message m WHERE m.id = :id")
Sign up to request clarification or add additional context in comments.

1 Comment

You can also just use an interface with the two getters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.