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
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring Rest + ResultSetExtractor problem " Watch "Spring Rest + ResultSetExtractor problem " New topic

Spring Rest + ResultSetExtractor problem

Spandana Anand

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> {

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

Joined: Mar 07, 2010
Posts: 1682

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

Joined: Feb 05, 2001
Posts: 17276

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.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
I agree. Here's the link:
subject: Spring Rest + ResultSetExtractor problem
It's not a secret anymore!