0

I am new to JAX-RS. I am doing CRUD operation using JAX_RS. CRUD operation is working fine for JSF but when I call fetchAll() method present in Service layer using JAX-RS I am getting a NullPointerException.

Below is my JAX_RS code

package com.resource;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.neorays.resource.entity.Person;
import com.neorays.resource.service.PersonService;

@Path("/persons")
public class MyResource {

    PersonService ser = new PersonService();

    @GET
    @Path("/pers")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Person> getAllPerson() {
        List<Person> list = null;
        try {
            list = ser.fetchAll();
            for (Person person : list) {
                person.getId();
                System.out.println("person id is" + person.getId());
                System.out.println("person name is" + person.getName());
                System.out.println("person country is" + person.getCountry());

            }
        } catch (NullPointerException ne) {
            System.out.println("null pt is occuring" + ne);
        }
        return list;
    }

}

This is my Service Layer

package com.resource.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neorays.resource.bean.PersonBean;
import com.neorays.resource.dao.PersonDao;
import com.neorays.resource.entity.Person;

@Component
@Transactional
public class PersonService 
{
    @Autowired
    private PersonDao dao;

    @Transactional
    public List<Person> fetchAll()
    {
        return dao.fetchAll();
    }
}

This is my DAO layer

package com.resource.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.neorays.resource.bean.PersonBean;
import com.neorays.resource.entity.Person;

@Repository
public class PersonDao 
{
    @Autowired
   private SessionFactory sessionFactory;

    public List<Person> fetchAll()
    {
        Session ses = sessionFactory.getCurrentSession();
        String query="from Person";
        Query query2 =ses.createQuery(query);
        List<Person> list = query2.list();

        return list;
    }
}

This is my web.xml

<?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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">


    <!-- needed for ContextLoaderListener -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</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.neorays.resource</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
</web-app>

This is my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.resource</groupId>
    <artifactId>jsf_jaxrs</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <build>
        <finalName>jsf_jaxrs</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
      </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <!-- uncomment this to get JSON support  -->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>

     <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>3.6.10.Final</version>
</dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.26</version>
    </dependency>
    <dependency>
        <groupId>javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.12.1.GA</version>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>3.2.10.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.2.10.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.faces</artifactId>
        <version>2.2.4</version>
    </dependency>
    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>el-api</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.2.10.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>3.2.10.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>3.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
    </dependency>

    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    </dependencies>

    <properties>
        <jersey.version>2.16</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

Finally, this is my Bean

package com.neorays.resource.entity;

import java.io.Serializable;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@Entity
@NamedQuery(name="Person.findAll", query="SELECT p FROM Person p")
public class Person implements Serializable {
    private static final long serialVersionUID = 1L;
    private int id;
    private String country;
    private String name;

    public Person() {
    }
    public Person(int id, String country, String name) {
        super();
        this.id = id;
        this.country = country;
        this.name = name;
    }
    @Id
    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }


    public String getCountry() {
        return this.country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 2
    You should be injecting the `PersonService` instead of creating a new one. – M. Deinum Aug 18 '15 at 06:28
  • Ya i did but iam getting the same Exception – Channabasava Gorebal Aug 18 '15 at 07:34
  • I highly doubt that... You might get a null pointer but that is probably because it isn't being injected, the source location is probably different. Make sure you have setup JAX-RS and the Spring integration correctly. – M. Deinum Aug 18 '15 at 07:35
  • Sorry for this silly question, Do i need to add/change any configurations to web.xml and pom.xml? – Channabasava Gorebal Aug 18 '15 at 07:50
  • I suggest a read of the [Jersey reference guide](https://jersey.java.net/documentation/latest/spring.html). I would also suggest not to mix spring versions (3.2.4 and 3.2.10). I also wonder why you are using Spring and not plain CDI when using JSF and JAX-RS... – M. Deinum Aug 18 '15 at 07:52
  • 1
    To clear out a conceptual mistake/misunderstanding: you don't "integrate JAX-RS in JSF". You just install it next to JSF in same webapp. Food for read: http://stackoverflow.com/questions/29982657/how-to-implement-jax-rs-restful-service-in-jsf-framework/ I removed the irrelevant JSF tag from the question. – BalusC Aug 18 '15 at 08:15

1 Answers1

1

Your dependency injection is not constructed correctly. All of your beans must be created by Spring. If your applicationContext.xml file scan necessary packages (for services and daos at least), and if your hibernate session factory created correctly and since you already tell jersey to scan package for rest services this should work.

@Component
@Path("/persons")
public class MyResource {

    @Autowired
    PersonService ser;
    ...

}

Update

I just realise package name in web.xml and your rest service are different.

<?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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
    ....

    <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.resource</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
    ....
</web-app>

If jersey-spring3 is missing in your class path you can add it using maven.

<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-spring3</artifactId>
    <version>${jersey.version}</version>
</dependency>

You can find additional info on jersey documentation.

bhdrkn
  • 6,244
  • 5
  • 35
  • 42