0

This is the mapping part:

@Path("/hello")
public class BookRestController {
    @Autowired
    BookService service;

    @GET
    public String getMsg() {

        System.out.println("in controller");
        List<BookDto> dto=service.loadAll();
        System.out.println(dto);
        return "dto";

    }

}

web.xml code:

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

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <display-name>springJdbc</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <description></description>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

servlet-dispatcher.xml code:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- controller package detail  -->
    <context:component-scan base-package="com.app.controller,com.app.service,com.app.dao.hib" />
    <!-- -->
    <mvc:annotation-driven/>
    <!--   -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:maxUploadSize="1500000"/>
    <!--  -->
    <mvc:resources mapping="/static/**" location="/static/" />
    <!-- view resolver  -->
    <bean id="viewResolver1" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/jsps/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    <!-- creating connection detail of hibernate (connection pooling) -->
    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/db1"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <!-- Hibernate mapping and configuration file details (Session factory details)  -->
    <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.app.entity.BookEntity</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <!--                <prop key="hibernate.hbm2ddl.auto">create</prop> -->
            </props>
        </property>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="mySessionFactory" />
    </bean>
</beans>

It's giving null pointer exception on service or anything I am using as autowired i.e it's having problems autowiring. Can anyone help with this?I guess i am missing something which is used to implement restFull service coz the application is working fine with simple spring application

u12345
  • 389
  • 2
  • 8
  • 17
  • Do you have the Implementation of ```BookService``` in your class path? – seenukarthi Oct 16 '14 at 08:00
  • Have you annotated your `BookRestController` with the `@Controller` annotation? And is your implemantation of `BookService` annotated with `@Service` annotation? – Jens Oct 16 '14 at 08:01
  • can you add @Produces along with GET eg:@Produces(MediaType.APPLICATION_JSON) – MrYo Oct 16 '14 at 08:02
  • Can you view this question? http://stackoverflow.com/questions/26377710/remove-from-api-call-when-optional-parameter-is-null – MrYo Oct 16 '14 at 08:05
  • @Karthikeyan yes i have made implementation of BookService and it is working fine when i am using in other spring controller – u12345 Oct 16 '14 at 08:18
  • In order for what you describing to work, you need to integration Spring with your JAX-RS implemenation (most-likely Jersey) – geoand Oct 16 '14 at 08:19
  • @Jens yes i have added but its still giving the same nullpointerException on service – u12345 Oct 16 '14 at 08:21
  • @Sanjaya i have added @Produces(MediaType.APPLICATION_JSON) but still same prob – u12345 Oct 16 '14 at 08:22
  • @Nup please add your spring config and your web.xml. – Jens Oct 16 '14 at 08:22
  • @geoand i have added jersey dependencies – u12345 Oct 16 '14 at 08:23
  • You still need to follow the integration instructions for you specific versions. For example http://www.mkyong.com/webservices/jax-rs/jersey-spring-integration-example/ – geoand Oct 16 '14 at 08:46
  • Please see this thread - http://stackoverflow.com/questions/19745187/spring-di-autowired-property-is-null-in-a-rest-service - which solves the problem. – Ashley Frieze Oct 16 '14 at 10:39
  • Please see this thread - http://stackoverflow.com/questions/19745187/spring-di-autowired-property-is-null-in-a-rest-service - which solves the problem. – Ashley Frieze Oct 16 '14 at 10:40

2 Answers2

1

Interface :

public interface BookService {

}

Class which implements interface BookService:

@Service
public class BookServiceImpl implements BookService {

}

To autowire BookServiceImpl class, you have to put @Autowired annotation on interface name in BookRestController class and @Controller annotation on your controller class:

@Controller
public class BookRestController {

@Autowired
private BookService bookService;

}

also you have to take care of configuration, like for component scanning you have to add following line in your spring configuration file. Put

<context:component-scan base-package="com.org.dao"/>

in spring configuration xml and change com.org.dao to your package name where dao files placed in application.

atish shimpi
  • 4,873
  • 2
  • 32
  • 50
  • i already did that its there in com.app.dao.hib package and i have added it in context:component-scan – u12345 Oct 16 '14 at 10:11
  • In your question @Controller not present in your BookRestController, can you provide your controller & service and service impl class. – atish shimpi Oct 16 '14 at 10:21
  • Actually the same code is working when i am not using jersey ie restFull service and rather just using it as a simple spring application so i dont think there can be problem in service service Impl class and i have added @Controller but it is still not working – u12345 Oct 16 '14 at 10:28
0

This is answered in this thread - Spring DI - Autowired property is null in a REST service

Essentially Jersey doesn't automatically do Spring autowiring for you. You need to get the Spring stuff to happen with the Jersey servlet object.

Community
  • 1
  • 1
Ashley Frieze
  • 4,993
  • 2
  • 29
  • 23