我试图将简单的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$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)
服务实施
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>
答案 0 :(得分:1)
您正在通过Spring进行自动装配,但您没有使用Spring Controller。请使用@Controller
或@RestController
为您的班级添加注释,并定义所需的REST路径。请查看有关Controllers
。
请从Spring官方文档中查看此示例here。
答案 1 :(得分:0)
嗯,我可能不正确,因为我对spring不太了解,但是要将XML作为响应返回,在模型中我使用Jersey Jax-rx时在模型类中使用@XmlRootElement注释。你可以尝试一下。但正如我所说,我不确定它是否会起作用。