• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Spring Rest + ResultSetExtractor problem

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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> {

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

Customer customer = null;
if(rs.next()){
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(rs.next()){} 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(rs.next()){} 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.
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

Mark
 
reply
    Bookmark Topic Watch Topic
  • New Topic