1

I am creating a REST application using Springboot. After doing some research I added JdbcTemplate to it rather than working directly with Jdbc and resultsets. I have the following configuration in application.properties.

server.context-path=/foo
spring.datasource.driverClassName=com.teradata.jdbc.TeraDriver
spring.datasource.url=jdbc:teradata://url
spring.datasource.username=root
spring.datasource.password=root

My REST controller has the following code

@RestController
public class LosController {

    @CrossOrigin
    @RequestMapping("/bar")
    public String Bar(){
        Gson gson = new Gson();
        Bar bar = new Bar();
        response = gson.toJson(bar.getData());
        return response;
    }

and in this object, I have

public class Bar {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<BarObject> getData(){
        String selectSql = "SELECT * FROM BAR";
        System.out.println(selectSql);
        System.out.println(jdbcTemplate.getDataSource().toString());

        List<BarObject> barObjs = jdbcTemplate.query(selectSql, new BarMapper());       

        return barObjs;
    }
}

I went through this link and configured everything as mentioned. I am able to see the System.out.println(selectSql) working. But at the next line, I am getting a null pointer exception. So the JdbcTemplate object isn't getting the data is what I feel. How can I get this working? I am trying to not use any xml configurations, which is the reason why I went for a properties file.

Community
  • 1
  • 1
mahacoder
  • 895
  • 3
  • 14
  • 29

1 Answers1

2

Bar is not a spring bean.

To get it working, you can annotate Bar with @Component and autowire it in LosController rather than creating with new.

@RestController
public class LosController {

    @Autowired
    private Bar bar;

    @CrossOrigin
    @RequestMapping("/bar")
    public String Bar(){
        Gson gson = new Gson();
        response = gson.toJson(bar.getData());
        return response;
    }
}

@Component
public class Bar {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    public List<BarObject> getData(){
        String selectSql = "SELECT * FROM BAR";
        System.out.println(selectSql);
        System.out.println(jdbcTemplate.getDataSource().toString());

        List<BarObject> barObjs = jdbcTemplate.query(selectSql, new BarMapper());       

        return barObjs;
    }
}
TheKojuEffect
  • 20,103
  • 19
  • 89
  • 125
  • that give me another error `nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency`. The full error message is bigger but from what I understand, it is not able to create the JdbcTemplate bean – mahacoder Mar 18 '16 at 09:04
  • check that the package where the implementation is located in is scanned (check your – ZaoTaoBao Mar 18 '16 at 09:16
  • @ZaoTaoBao implementation of what? My application class is just annotated with `@SpringBootApplication` – mahacoder Mar 18 '16 at 09:19
  • sorry I don't see it..maybe spring doesn't know which bean inject...put @qualifier("jdbcTemplate") just for test.. – ZaoTaoBao Mar 18 '16 at 09:45