I am having trouble to get the data that I retrieve from MySQL database into a list of objects. I want to return the data and store it into the list. Once I do that, I want the data available for retrieval to a frontend framework.
But I get an incompatible type error, I cannot return the data into the list.
Note: I am relatively new to Spring-Boot and JdbcTemplate.
My code
This is inside my rest controller
@Autowired
private JdbcTemplate temp;
@RequestMapping("/foo")
public List<Foo> connectAndRetrieve() {
String sql = "SELECT fooName FROM Foo;";
// Error Here
List<Foo> data = temp.queryForList(sql, Foo.class);
return data;
}
How do I return the data from the method and use that data?
Thanks!
You are actually projecting fooName
instead of Foo
, try the following:
@Autowired
private JdbcTemplate temp;
@RequestMapping("/foo")
public List<Foo> connectAndRetrieve() {
String sql = "SELECT * FROM Foo";
return temp.query(sql, new BeanPropertyRowMapper(Foo.class));
}
Here you can find the documentation for BeanPropertyRowMapper