Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A RowMapper for auto-mapping JDBC ResultSet columns to Jackson ObjectNode fields [SPR-9250] #13888

spring-projects-issues opened this issue Mar 19, 2012 · 2 comments
in: data status: declined type: enhancement


Copy link

@spring-projects-issues spring-projects-issues commented Mar 19, 2012

Keith Donald opened SPR-9250 and commented

Similar in spirit to the existing BeanPropertyRowMapper, but instead of mapping ResultSet columns to POJOs, we would map directly to Jackson ObjectNode instances.

The main use case is JSON serialization as part of a REST API implementation. In many cases mapping to a JavaBeans model just to write JSON is overkill. It can be a lot simpler to simply convert a JDBC ResultSet to a ObjectNode or List of ObjectNodes. This can be done at present by hand (see (1) below), but this could be automated in many scenarios with a built-in JsonNodeRowMapper implementation that is similar in spirit to BeanPropertyRowMapper.

(1) Manual ResultSet-to-JsonNode mapping example

    @RequestMapping(value="/me/franchises", method=RequestMethod.GET, params="league", produces="application/json")
    public ResponseEntity<? extends Object> qualifyingFranchises(@RequestParam Integer league) {
        if (!SecurityContext.playerSignedIn()) {
            return new ResponseEntity<ErrorBody>(new ErrorBody("not authorized"), HttpStatus.FORBIDDEN);
        List<JsonNode> franchises = jdbcTemplate.query(qualifyingFranchisesSql, new RowMapper<JsonNode>() {
            public JsonNode mapRow(ResultSet rs, int rowNum) throws SQLException {
                ObjectNode franchise = mapper.createObjectNode();
                franchise.put("id", rs.getInt("id"));
                franchise.put("name", rs.getString("name"));                
                return franchise;
        }, SecurityContext.getCurrentPlayer().getId(), league);
        return new ResponseEntity<List<JsonNode>>(franchises, HttpStatus.ACCEPTED);

Affects: 3.1.1

Reference URL:!/kdonald/status/181918401042726913

1 votes, 2 watchers

Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Mar 20, 2012

Chris Beams commented

Hey Keith,

Sounds like a good idea, and would be a perfect chunk of work for a pull request, whether from you or someone else.

Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Mar 20, 2012

Keith Donald commented

Basic JsonRowMapper implementation:

@spring-projects-issues spring-projects-issues added status: declined in: data type: enhancement labels Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
in: data status: declined type: enhancement
None yet

No branches or pull requests

2 participants