We are using ObjectMapper. When using ObjectMapper with RowMapper, should it be declared inside each mapRow (seen below), or outside of mapRow as a class public member? I assume it should be outside as a public class member per this article. Should I declare Jackson's ObjectMapper as a static field?
Currently using Spring boot with SQL Server database. Researching thread safety with thousands/millions of sql rows its getting.
List<Product> productList = namedParameterJdbcTemplate.query(sqlQuery,
parameters,
new ProductMapper(productRequest));
public class ProductMapper implements RowMapper<Product> {
@Override
public Product mapRow(ResultSet rs, int rowNum) throws SQLException {
ObjectMapper objectMapper = new ObjectMapper()
Product product = new Product();
product.setProductId(rs.getLong("ProductId"));
product.setProductType(rs.getString("ProductType"));
product.setLocations(objectMapper.readValue(rs.getString("Locations"), new TypeReference<List<ServiceLocation>>(){}));
} catch (Exception e) {
throw new ServiceException(e);
}
}
Note: Please don't ask why we are writing this manual mapper with ObjectMapper, we are doing legacy coding, and architects requested to do this.