RESTful 应用程序未按预期运行。抛出空指针异常

RESTful application is not working as desired. Throwing null pointer exception

我正在尝试将简单的 CRUD 应用程序配置为 RESTful 配置。应用程序返回空指针异常而不是 XML 输出。它与数据库连接有什么关系吗?我检查了应用程序,它运行正常。

终点

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);
    }

}

错误

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.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)

服务实施

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 球衣部分

<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>

您正在通过 Spring 自动装配某物,但您没有使用 Spring 控制器。请用 @Controller@RestController 注释您的 class 并定义您想要的 REST 路径。请查看 Spring 有关 Controllers 的文档。

请从官方 Spring 文档中查看此示例 here

好吧,我可能不正确,因为我对 spring 了解不多,但是对于 return XML 作为回应,在模型中我使用 @XmlRootElement 注释 class 使用 Jersey Jax-rx 时。你可以试试。但正如我所说,我不确定它是否有效。