0
votes

I'm trying to @autowire my jersey resource class with my mapper interface and it's giving null pointer annotation exception but @autowire works fine in my dispatcher controller. I'm new to spring mvc and I just want a way to @autowire my jersey resource class with my mapper interface.

Controller code

@Controller
public class EmployController {
    @Autowired
    EmployMapper employMapper;

    @RequestMapping("test")
    public ModelAndView reviewspage()
    {
        employMapper.updateMytable("SpringController");
          ModelAndView modelAndView = new ModelAndView("test");
            return modelAndView;
    }
}

jersey resource class

@Path("/employees")
@Repository
public class MyResource {
    @Autowired
    EmployMapper employMapper;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getEmployees(){
        employMapper.updateMytable("WebAPiController");
        return "Successfuly done";
    }
}

Mapper interface

public interface EmployMapper {
    public int finduser(@Param("MName") String MName,@Param("password") String password);
    public int updateMytable(@Param("name") String name);
}

Spring dispatcher xml

<beans 
   <mvc:annotation-driven />
   <context:component-scan base-package="com.controller"/>
    <bean id = "ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" 
      value="/WEB-INF/" />
      <property name="suffix"
       value=".jsp" />
   </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost/employtrackingsystem"/>
    <property name="username" value="adminXuMPZTn" />
    <property name="password" value="IMhzT-AJE47P" />
  </bean>


   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     <property name="dataSource" ref="dataSource" />
     <property name="mapperLocations" value="classpath*:*mappers/EmployMapper.xml"/>

</bean>


  <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
      <constructor-arg index="0" ref="sqlSessionFactory" />
 </bean>

 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.mappers"/>
</bean>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         metadata-complete="false">

         <servlet>
    <servlet-name>spring-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>



           <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.resources</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
     <context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>false</param-value>
</context-param>
<context-param>
    <param-name>resteasy.scan.providers</param-name>
    <param-value>false</param-value>
</context-param>
<context-param>
    <param-name>resteasy.scan.resources</param-name>
    <param-value>false</param-value>
</context-param>


</web-app>
1
share your web.xml - Vaibs
@Vaibs I have added my web.xml above kindly check it - Awais Ahmad
See my comment below: your web.xml doesn't instruct Jersey to use Spring. - marthursson

1 Answers

1
votes

My guess is that you have not configured Jersey to delegate object lifecycle to Spring, using jersey-spring. If this has not been done explicitly, Jersey itself will control the lifecycle, which means that it will be Jersey that instantiates your resource class.