The Jersey service class appears as an interface java.lang.NoSuchMethodException
When using jersey, the class that provided the service was designed as an interface, configured in Web.xml, and the Access service encountered an exception after running Tomcat:
Java.lang.NoSuchMethodException:Could not find a suitable constructor in Coursemanager.resource.CourseResource class.
Scenario
The Bean used:
Package Coursemanager.bean;
/** * Course/public class Course {/** * Course ID */private Integer ID;
/** * Course name/private String name;
/** * Professor Teacher/private String teacher;
/** * Classroom Location * * Private String location;
/** * Number of Students * * Private Integer studentnum;
Public Integer GetId () {return id;
The public void SetId (Integer id) {this.id = ID;
Public String GetName () {return name;
public void SetName (String name) {this.name = name;
Public String Getteacher () {return teacher;
} public void Setteacher (String teacher) {this.teacher = teacher;
Public String getLocation () {return location;
} public void setlocation (String location) {this.location = location;
Public Integer Getstudentnum () {return studentnum; } public void Setstudentnum (inTeger studentnum) {this.studentnum = Studentnum;
Public Course () {} public Course (string name, string teacher, string location, Integer studentnum) {
Super ();
THIS.name = name;
This.teacher = teacher;
this.location = location;
This.studentnum = Studentnum;
Public Course (integer ID, string name, string teacher, string location, Integer studentnum) {super ();
This.id = ID;
THIS.name = name;
This.teacher = teacher;
this.location = location;
This.studentnum = Studentnum; @Override public String toString () {return "Course [id= + ID +", name= "+ name +", teacher= "+ TEAC
Her + ", location=" + location + ", studentnum=" + Studentnum + "];
}
}
Declare an interface for a service:
Package Coursemanager.resource;
Import Javax.ws.rs.Consumes;
Import Javax.ws.rs.DELETE;
Import Javax.ws.rs.GET;
Import Javax.ws.rs.POST;
Import Javax.ws.rs.PUT;
Import Javax.ws.rs.Path;
Import Javax.ws.rs.PathParam;
Import javax.ws.rs.Produces;
Import Javax.ws.rs.QueryParam;
Import Javax.ws.rs.core.MediaType;
Import Coursemanager.bean.Course; /** * Course Resources/@Path ("course") public interface Courseresource {/** * Save course Information * Receive JSON information * return The resource address of the course * @param course course * @return The ID of the successful new course, failure returns to "error"/@POST @Consumes (mediatype.application_
JSON) @Produces (mediatype.text_plain) public Integer Save (Course Course);
/** * Delete course * @param ID Course ID * @return successfully returned "Success", Failed to return "error"/@DELETE @Path ("{id:\\d+}")
Public String Delete (@PathParam ("id") Integer ID); /** * Update course Information * @param Course Course * @return successfully returned to "success", Failed to return "error" * * * @PUT @Consumes (Mediatyp E.application_jsoN) @Produces (mediatype.text_plain) public String update (Course Course); /** * Get the course information for a specific ID * @param ID Course ID * @return Course * * @GET @Path ("{id:\\d+}") @Produces (Media
Type.application_json) public Course Get (@PathParam ("id") Integer ID); /** * Inquiry Course information through the course name * @param name * @return/@GET @Produces (Mediatype.application_json) p
Ublic Course Getbycoursename (@QueryParam ("name") String name);
}
Implementation classes for interfaces:
Package Coursemanager.impl;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;
Import Coursemanager.bean.Course;
Import Coursemanager.resource.CourseResource;
public class Courseresourceimpl implements Courseresource {public static Integer CourseID = 0;
public static list<course> courses = new arraylist<course> ();
Public Courseresourceimpl () {System.out.println ("Initialize 5 records"); Add 5 records for (int i = 1; I <= 5; i++) {Course Course = new Course ("course" + I, "teacher" + I, "place" + I, 5
0);
Addcourse (course);
@Override Public Integer Save (Course Course) {addcourse (Course);
return Course.getid ();
@Override Public String Delete (Integer ID) {iterator<course> iter = Courses.iterator ();
while (Iter.hasnext ()) {Course Course = Iter.next (); if (Course.getid (). Equals (ID)) {courses.reMove (course);
courseid--;
Return "Success";
} return "error";
@Override Public String Update (Course Course) {iterator<course> iter = Courses.iterator ();
while (Iter.hasnext ()) {Course co = iter.next ();
if (Co.getid (). Equals (Course.getid ())) {Courses.remove (CO);
Courses.add (course);
Return "Success";
} return "error";
@Override public Course get (Integer ID) {iterator<course> iter = Courses.iterator ();
while (Iter.hasnext ()) {Course Course = Iter.next ();
if (Course.getid (). Equals (ID)) {return course;
} return null; @Override public Course getbycoursename (String name) {iterator<course> iter = Courses.iterator ()
;
while (Iter.hasnext ()) { Course Course = Iter.next ();
if (Course.getname (). Equals (name)) {return course;
} return null;
} private void Addcourse (Course Course) {courseid++;
Course.setid (CourseID);
Courses.add (course);
}
}
Configuration in Web.xml:
<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>coursemanager</ param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet >
<servlet-mapping>
<servlet-name>jersey Web application</servlet-name>
< Url-pattern>/*</url-pattern>
</servlet-mapping>
Accessing HTTP://LOCALHOST:8080/COURSEMANAGER/COURSE/1, triggering an exception
Serious: Servlet.service () for Servlet [Jersey Web application] in context with path [/coursemanager] threw exception [A Multie Xception has 1 exceptions. They are:1. Java.lang.NoSuchMethodException:Could not find a suitable constructor in Coursemanager.resource.CourseResou
Rce class. ] with the root cause java.lang.NoSuchMethodException:Could not find a suitable constructor in Coursemanager.resource.CourseR
Esource class. At Org.glassfish.jersey.internal.inject.JerseyClassAnalyzer.getConstructor (jerseyclassanalyzer.java:192) at Org.jvnet.hk2.internal.Utilities.getConstructor (utilities.java:178) at Org.jvnet.hk2.internal.Utilities.justCreate (utilities.java:988) at Org.jvnet.hk2.internal.ServiceLocatorImpl.create (servicelocatorimpl.java:962) at Org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize (servicelocatorimpl.java:1054) at Org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize (servicelocatorimpl.java:1046) at Org.glassfish.jersey.internal.inject. Injections.getorcreate (injections.java:173) at org.glassfish.jersey.server.model.methodhandler$ Classbasedmethodhandler.getinstance (methodhandler.java:284) at Org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply (pushmethodhandlerrouter.java:74) at Org.glassfish.jersey.server.internal.routing.routingstage._apply (routingstage.java:109) at Org.glassfish.jersey.server.internal.routing.routingstage._apply (routingstage.java:112) at Org.glassfish.jersey.server.internal.routing.routingstage._apply (routingstage.java:112) at Org.glassfish.jersey.server.internal.routing.routingstage._apply (routingstage.java:112) at Org.glassfish.jersey.server.internal.routing.routingstage._apply (routingstage.java:112) at Org.glassfish.jersey.server.internal.routing.RoutingStage.apply (routingstage.java:92) at Org.glassfish.jersey.server.internal.routing.RoutingStage.apply (routingstage.java:61) at Org.glassfish.jersey.process.internal.Stages.process (Stages.java:197) at Org.glassfish.jersey.server.serverruntime$2.run (serverruntime.java:318) at org.glassfish.jersey.in Ternal. Errors$1.call (errors.java:271) at Org.glassfish.jersey.internal.errors$1.call (errors.java:267) at Org.glassfish.je Rsey.internal.Errors.process (errors.java:315) at org.glassfish.jersey.internal.Errors.process (errors.java:297) at Org.glassfish.jersey.internal.Errors.process (errors.java:267) at Org.glassfish.jersey.process.internal.RequestScope.runInScope (requestscope.java:317) at Org.glassfish.jersey.server.ServerRuntime.process (serverruntime.java:305) at Org.glassfish.jersey.server.ApplicationHandler.handle (applicationhandler.java:1154) at Org.glassfish.jersey.servlet.WebComponent.serviceImpl (webcomponent.java:473) at Org.glassfish.jersey.servlet.WebComponent.service (webcomponent.java:427) at Org.glassfish.jersey.servlet.ServletContainer.service (servletcontainer.java:388) at Org.glassfish.jersey.servlet.ServletContaiNer.service (servletcontainer.java:341) at Org.glassfish.jersey.servlet.ServletContainer.service ( servletcontainer.java:228) at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter ( applicationfilterchain.java:292) at Org.apache.catalina.core.ApplicationFilterChain.doFilter ( applicationfilterchain.java:207) at Org.apache.tomcat.websocket.server.WsFilter.doFilter (wsfilter.java:52) at Org. Apache.catalina.core.ApplicationFilterChain.internalDoFilter (applicationfilterchain.java:240) at Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:207) at Coursemanager.filter.EncodingFilter.doFilter (encodingfilter.java:36) at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (applicationfilterchain.java:240) at Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:207) at Org.apache.catalina.core.StandardWrapperValve.invoke (standardwrappervalve.java:212) at Org.apache.cataLina.core.StandardContextValve.invoke (standardcontextvalve.java:106) at Org.apache.catalina.authenticator.AuthenticatorBase.invoke (authenticatorbase.java:502) at org