使用jersey

时间:2015-07-01 18:20:05

标签: java web-services rest jersey

提前感谢您的回答。我正在开发RESTful webservice库,我想使用接口公开服务。我在运行应用程序时遇到错误: “javax.servlet.ServletException:MultiException有1个例外。它们是: 1. java.lang.NoSuchMethodException:在RESTful.library.endpoint.ManageabilityEndpoint类中找不到合适的构造函数。“

接口与资源不同,但我导入了资源包。相关的源代码是下一个:

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

}

接口是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);

}

我没有在代码中找到任何注释。您知道如何使用界面调用资源吗?

提前感谢您的帮助。

干杯

0 个答案:

没有答案