-1

I'm trying to configure simple CRUD application as a RESTful configuration. The application is returning null pointer exception instead of XML output. Does it have to anything with being the database connection? I checked the application and it's working normally.

End Point

package com.naveen.end_point;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;

import com.naveen.model.Student;
import com.naveen.service.StudentService;

@Path("/student")
public class StudentRESTfulClient {

    @Autowired
    StudentService studentService;

    public StudentRESTfulClient() {

    }

    @DELETE
    @Path("/delete/{id}")
    @Produces(MediaType.APPLICATION_XML)
    public Response deleteStudent(@PathParam("id") long id) {
        studentService.removeStudent(id);
        return Response.ok().build();
    }

    @GET
    @Path("/list")
    @Produces(MediaType.APPLICATION_XML)
    public List<Student> getStudentsList() {
        List<Student> list = studentService.getStudentsList();
        return list;
    }

    @POST
    @Path("/add")
    @Consumes(MediaType.APPLICATION_XML)
    public Response addStudent(Student s) {
        studentService.addStudent(s);
        return Response.ok().build();
    }

    @GET
    @Path("/studentsById/{id}")
    @Produces(MediaType.APPLICATION_XML)
    public Student getStudentById(@PathParam("id") long id) {
        return studentService.getStudentById(id);
    }

}

Error

java.lang.NullPointerException
    com.naveen.end_point.StudentRESTfulClient.getStudentsList(StudentRESTfulClient.java:44)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
    com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
    com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
    com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Service Implementation

package com.naveen.service;

import java.util.List;

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

import com.naveen.dao.StudentDao;
import com.naveen.model.Student;

@Service
@Transactional
@EnableTransactionManagement
public class StudentServiceImpl implements StudentService {

    @Autowired
    StudentDao studentDao;

    @Override
    public List<Student> getStudentsList() {

        return studentDao.findAll();

    }

    @Override
    public void addStudent(Student s) {
        studentDao.saveStudent(s);
    }

    @Override
    public void updateStudent(Student p) {
        studentDao.updateStudent(p);
    }

    @Override
    public void removeStudent(long id) {
        studentDao.deleteStudent(id);
    }

    @Override
    public Student getStudentById(long id) {
        return studentDao.findOne(id);
    }

}

Web.xml Jersey part

<servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.naveen</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/restful/*</url-pattern>
    </servlet-mapping>
Hadrian Blackwater
  • 437
  • 2
  • 5
  • 15

2 Answers2

1

You are autowiring sth via Spring but you are not using a Spring Controller. Please annotate your class with @Controller or @RestController and define the REST paths you want. Please have a look at Spring documentation about Controllers.

Please check this example here from the official Spring documentation.

Apostolos
  • 10,033
  • 5
  • 24
  • 39
  • I haven't learned how to develop RESTful web services with Spring framework yet. Is there any way to get it to work like a regular Jersey based RESTful application? – Hadrian Blackwater Jul 26 '16 at 06:11
  • dont mix technologies. if you want to use jersey, find the equivalent tutorial or go the Spring-official way :) – Apostolos Jul 26 '16 at 06:15
  • Hello Apostolos, I think the studentService is null here. But I need to know why. Please let me know what could be the reason? – Hadrian Blackwater Jul 26 '16 at 06:16
  • but... i just explained to you why! :) annotate your client with `@Controller` and check again. go the Spring-official way! – Apostolos Jul 26 '16 at 06:17
  • Alright then, I'll try to configure it the Spring way. Do I have to change the web.xml and dependencies? – Hadrian Blackwater Jul 26 '16 at 06:17
  • it's all in the link i posted. it's very clear and you will understand everything. i propose you start a new project so as not to mess up with a lot of changes. clean new project and step by step reading and configuration will be the best option imho :) – Apostolos Jul 26 '16 at 06:21
  • But I don't want to use spring boot to do it. Could I do it with spring mvc? – Hadrian Blackwater Jul 26 '16 at 06:22
  • yes, spring boot is just an easier step of configuration. you can find lots of examples with spring mvc and rest controllers, but i see that they use Spring Boot everywhere. even I, using Spring mvc for years, think of changing to Boot too :) – Apostolos Jul 26 '16 at 06:25
  • please also consider accepting an answer, if it helped you fix your problem :) – Apostolos Jul 26 '16 at 06:35
  • @HadrianBlackwater did my answer help you? please accept it so as to consider this question closed. thank you! – Apostolos Jul 28 '16 at 05:34
0

Well I might be not correct as i don't know much about spring, But to return XML as response , in model i use @XmlRootElement annotation in model class when using Jersey Jax-rx . you can try it. but as i said m not sure it will work or not.

subro
  • 104
  • 2
  • 15