Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Spring Rest + ResultSetExtractor problem  RSS feed

Spandana Anand
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi I am implementing rest services using spring 3 and also for data fetching using jdbctemplate.
Currently i have to fetch a record from customer table based upon a customer id passed.

For same i have used : jdbcTemplate.query(SqlQueries.SQL_SELECT_CUSTOMER_ BY_TOKEN, new Object[]{customerToken}, new CustomerExtractor());

CustomerExtractor is a private class implementing ResultSetExtractor

code snippet:

private class CustomerExtractor implements ResultSetExtractor<Customer> {

public Customer extractData(ResultSet rs) throws SQLException,
DataAccessException {

Customer customer = null;
customer = new Customer();
//populate customer
return customer;

PS - customer with provided id may or may not exist.

Here when I access rest service using web browser , i don;t get valid customer even if customer id sent as pathVariable is correct.
Upon debugging i found that debug pointer does not go inside the if({} block

There is nothing wrong in query it works fine in mysql workbench.

Also I implemented rest client using apacheHttpClient now when i used this test class to test restservice i got valid response from service

Upon debugging i found that debug pointer goes inside the if({} block

kindly explain why there is such strange behaviour that when accessed via browser i don't get valid response and when tested via test class I get a valid response.
Bill Gorder
Posts: 1682
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use Code tags so that we can read the code you post.

The code you posted seems at this point to be irrelevant to your problem, as you have stated that you are hitting a rest service with a browser and not getting a response. I think a good place to start would be showing us the relevant piece of your controller.
Mark Spritzler
Posts: 17309
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd also post your query.

Based on very little information, I would guess that you actually should implement a RowMapper, not a ResultSetExtractor. A ResultSetExtractor would be used if your query has many joins to many tables, so complex queries where you have to build up a big complex object graph. It looks like you are running a query that just goes to your Customer table and returns only results from that one table, where the values from that table map one to one to a Customer object. For those simple queries you should use a RowMapper.

RowMapper has one method mapRow(ResultSet rs, int row)

So if you run a query that returns one row, or a query returning 10 rows where each row is mapped to one Customer object, then RowMapper should be used, one big difference is with RowMapper you don't have to loop through the resultset, or check if there is a row or a nextRow.

Here is an example of a RowMapper

I can re-use it for many queries, one that returns 1 row, one that returns more than one row. Where each Row I need a Customer object for that row.

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!