This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Spring and the fly likes Spring Rest + ResultSetExtractor problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring Rest + ResultSetExtractor problem " Watch "Spring Rest + ResultSetExtractor problem " New topic
Author

Spring Rest + ResultSetExtractor problem

Spandana Anand
Greenhorn

Joined: Jun 26, 2012
Posts: 3
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.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

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.


[How To Ask Questions][Read before you PM me]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
Don't get me started about those stupid light bulbs.
 
subject: Spring Rest + ResultSetExtractor problem
 
Similar Threads
Understanding how to write spring ws using JAXB
@Autowired not working in JUnit using spring 2.5
hibernate one to many reationship.
doubts from jsp spec
Binding Endpoint to HTTPS problem