Thank you in advance for your answers. I am working on a RESTful webservice library and I want to expose the services using an interface. I am getting an error when running the application: "javax.servlet.ServletException: A MultiException has 1 exceptions. They are: 1. java.lang.NoSuchMethodException: Could not find a suitable constructor in RESTful.library.endpoint.ManageabilityEndpoint class."
The interface is in different package than the resource, but I imported the package of the resource. The source code related is the next:
BookResource.java
package RESTful.library.resources;
import RESTful.library.endpoint.*;
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 RESTful.library.service.BookService;
import RESTful.library.endpoint.ManageabilityEndpoint;
import RESTful.library.model.Books;
/** This class will implement all the request on the resource:
* GET
* PUT
* DELETE
* POST
*/
public class BookResource implements ManageabilityEndpoint{
DataBaseSQLite db = new DataBaseSQLite();
BookService bookService = new BookService();
/* (non-Javadoc)
* @see RESTful.library.resources.ManageabilityEndpoint#getBooks()
*/
@Override
public List<Books> getBooks(){
List<Books> books = bookService.getAllBooks();
return books;
}
/* (non-Javadoc)
* @see RESTful.library.resources.ManageabilityEndpoint#getBook(int)
*/
@Override
public Books getBook(@PathParam("bookID") int ID){
return bookService.getBook(ID);
}
/* (non-Javadoc)
* @see `RESTful.library.resources.ManageabilityEndpoint#addBook(RESTful.library.model.Books)`
*/
@Override
public Books addBook(Books book) {
if (book != null)
return bookService.addBook(book);
return null;
}
/* (non-Javadoc)
* @see RESTful.library.resources.ManageabilityEndpoint#removeBook(int)
*/
@Override
public String removeBook(@PathParam("bookID") int ID){
boolean removed= bookService.deleteBook(ID);
String answer="Removed successfully";
if(removed = false){
answer="Not removed";
}
return answer;
}
/* (non-Javadoc)
* @see RESTful.library.resources.ManageabilityEndpoint#removeBook(java.lang.String)
*/
@Override
public String removeBook(@PathParam("bookName") String name){
boolean removed= bookService.deleteBook(name);
String answer="Removed successfully";
if(removed = false){
answer="Not removed";
}
return answer;
}
/* (non-Javadoc)
* @see RESTful.library.resources.ManageabilityEndpoint#removeBookYear(int)
*/
@Override
public String removeBookYear(@PathParam("bookYear") int year){
boolean removed= bookService.deleteBookYear(year);
String answer="Removed successfully";
if(removed = false){
answer="Not removed";
}
return answer;
}
/* (non-Javadoc)
* @see RESTful.library.resources.ManageabilityEndpoint#removeBookAuthor(java.lang.String)
*/
@Override
public String removeBookAuthor(@PathParam("bookAuthor") String author){
boolean removed= bookService.deleteBookAuthor(author);
String answer="Removed successfully";
if(removed = false){
answer="Not removed";
}
return answer;
}
/* (non-Javadoc)
* @see RESTful.library.resources.ManageabilityEndpoint#removeBookPublisher(java.lang.String)
*/
@Override
public String removeBookPublisher(@PathParam("bookPublisher") String publisher){
boolean removed= bookService.deleteBookPublisher(publisher);
String answer="Removed successfully";
if(removed = false){
answer="Not removed";
}
return answer;
}
/* (non-Javadoc)
* @see RESTful.library.resources.ManageabilityEndpoint#findBook(java.lang.String)
*/
@Override
public Books findBook(@PathParam("bookName") String name) {
if (name != null)
return bookService.findBook(name);
return null;
}
/* (non-Javadoc)
* @see RESTful.library.resources.ManageabilityEndpoint#getBooksByYear(int)
*/
@Override
public List<Books> getBooksByYear(@PathParam("bookYear")int year){
List<Books> books = bookService.getAllBooksByYear(year);
return books;
}
/* (non-Javadoc)
* @see RESTful.library.resources.ManageabilityEndpoint#getBooksByAuthor(java.lang.String)
*/
@Override
public List<Books> getBooksByAuthor(@PathParam("bookAuthor")String author){
List<Books> books = bookService.getAllBooksByAuthor(author);
return books;
}
/* (non-Javadoc)
* @see RESTful.library.resources.ManageabilityEndpoint#getBooksByPublisher(java.lang.String)
*/
@Override
public List<Books> getBooksByPublisher(@PathParam("bookPublisher")String publisher){
List<Books> books = bookService.getAllBooksByPublisher(publisher);
return books;
}
}
The interface is manageabilityEndpoint.java:
package RESTful.library.endpoint;
import RESTful.library.resources.*;
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 RESTful.library.model.Books;
@Path("/books")
public interface ManageabilityEndpoint {
@GET
@Produces(MediaType.APPLICATION_JSON)
public abstract List<Books> getBooks();
@GET
@Path("/{bookID}")
@Produces(MediaType.APPLICATION_JSON)
public abstract Books getBook(int ID);
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public abstract Books addBook(Books book);
@DELETE
@Path("/{bookID}")
@Produces(MediaType.APPLICATION_JSON)
public abstract String removeBook(int ID);
@DELETE
@Path("/name/{bookName}")
@Produces(MediaType.APPLICATION_JSON)
public abstract String removeBook(String name);
@DELETE
@Path("/year/{bookYear}")
@Produces(MediaType.APPLICATION_JSON)
public abstract String removeBookYear(int year);
@DELETE
@Path("/author/{bookAuthor}")
@Produces(MediaType.APPLICATION_JSON)
public abstract String removeBookAuthor(String author);
@DELETE
@Path("/publisher/{bookPublisher}")
@Produces(MediaType.APPLICATION_JSON)
public abstract String removeBookPublisher(String publisher);
@GET
@Path("/name/{bookName}")
@Produces(MediaType.APPLICATION_XML)
public abstract Books findBook(String name);
@GET
@Path("/year/{bookYear}")
@Produces(MediaType.APPLICATION_JSON)
public abstract List<Books> getBooksByYear(int year);
@GET
@Path("/author/{bookAuthor}")
@Produces(MediaType.APPLICATION_JSON)
public abstract List<Books> getBooksByAuthor(String author);
@GET
@Path("/publisher/{bookPublisher}")
@Produces(MediaType.APPLICATION_JSON)
public abstract List<Books> getBooksByPublisher(String publisher);
}
I haven't found any annotation to include in the code. Do you know how can I call the resource using an interface?
Thank you in advance for your help.
Cheers