Servlet.service() for servlet [dispatcher] in context with path [/***] threw exception

We Are Going To Discuss About Servlet.service() for servlet [dispatcher] in context with path [/***] threw exception. So lets Start this Java Article.

Servlet.service() for servlet [dispatcher] in context with path [/***] threw exception

  1. Servlet.service() for servlet [dispatcher] in context with path [/***] threw exception

    Although you have defined the jdbcUserDAO bean, you have not wired it into the UserController class. This causes a NullPointerException because the jdbcUserDAO field is null, which results in the exception when jdbcUserDAO.getAllUsers() is called. To solve this, annotate the jdbcUserDAO setter in the UserController with @Autowired, as in:

  2. Servlet.service() for servlet [dispatcher] in context with path [/***] threw exception

    Although you have defined the jdbcUserDAO bean, you have not wired it into the UserController class. This causes a NullPointerException because the jdbcUserDAO field is null, which results in the exception when jdbcUserDAO.getAllUsers() is called. To solve this, annotate the jdbcUserDAO setter in the UserController with @Autowired, as in:

Solution 1

Although you have defined the jdbcUserDAO bean, you have not wired it into the UserController class. This causes a NullPointerException because the jdbcUserDAO field is null, which results in the exception when jdbcUserDAO.getAllUsers() is called. To solve this, annotate the jdbcUserDAO setter in the UserController with @Autowired, as in:

@RestController
public class UserController {

    private JdbcUserDAO jdbcUserDAO;

    public JdbcUserDAO getJdbcUserDAO() {
        return jdbcUserDAO;
    }

    @Autowired
    public void setJdbcUserDAO(JdbcUserDAO jdbcUserDAO) {
        this.jdbcUserDAO = jdbcUserDAO;
    }

    @RequestMapping("/")
    @ResponseBody
    public String welcome() {
        return "Welcome to RestTemplate Example.";
    }

    @RequestMapping(value="/users", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody   
    public List<User> getAllUsers() {
        System.out.println("jdbcUserDAO"+jdbcUserDAO);
        List<User> userList = jdbcUserDAO.getAllUsers();
        System.out.println(userList);
        return userList;
    }

}

The @Autowired annotation tells Spring that a bean that matches the interface of the parameter for the setter (in this case, JdbcUserDAO) should be injected into the setter, which results in the jdbcUserDAO field being set to your JdbcUserDAO bean defined in application_config.xml.

You could also simplify your UserController and annotate the field (removing the getter and setter):

@RestController
public class UserController {

    @Autowired
    private JdbcUserDAO jdbcUserDAO;

    @RequestMapping("/")
    @ResponseBody
    public String welcome() {
        return "Welcome to RestTemplate Example.";
    }

    @RequestMapping(value="/users", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody   
    public List<User> getAllUsers() {
        System.out.println("jdbcUserDAO"+jdbcUserDAO);
        List<User> userList = jdbcUserDAO.getAllUsers();
        System.out.println(userList);
        return userList;
    }

}

For more information, see Guide to Spring @Autowired.


It also looks like your application_config.xml is not being included in Spring’s application context. To include this XML file, change your ApplicationConfiguration class to resemble the following:

@ComponentScan(basePackages = {"com.ganeshTrading.riceAppService"})
@Configuration
@EnableWebMvc
@ImportResource("classpath:/path/to/application_config.xml")
public class ApplicationConfiguration {

}

Note that classpath:/path/to/application_config.xml is an example and the actual location of your application_config.xml file should be used instead.

Original Author Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment