尝试使用Postman POST到REST时出现异常

时间:2016-11-07 13:08:18

标签: java rest jersey jax-rs

在这里,我尝试使用邮递员为“休息应用程序”提供“表单数据”,但我在下面面对此例外。

Postman Client< - 检查此图片并告诉我,如果我在这里做错了什么

HTTP Status 500 - Servlet.init() for servlet jersey threw exception
type Exception report message Servlet.init() for servlet jersey threw exception description The server encountered an internal error that prevented it from fulfilling this request. 

exception

javax.servlet.ServletException: Servlet.init() for servlet jersey threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Unknown Source)
root cause

com.sun.jersey.spi.inject.Errors$ErrorMessagesException
    com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
    com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
    com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:795)
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:790)
    com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:509)
    com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:339)
    com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605)
    com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:207)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:394)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:577)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    org.apache.catalina.valves.Err[enter image description here][1]orReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Unknown Source)
note The full stack trace of the root cause is available in the Apache Tomcat/9.0.0.M11 logs.

Apache Tomcat/9.0.0.M11

这是我的类做的POST,我已经把所有的参数作为'create'方法中的字符串,然后我根据需要将它们分别解析为'double'和'int'。 (如果有任何其他方法直接解析整数,请告诉我。)

        package com.glarimy.lib.rs;
        import javax.ws.rs.Consumes;
        import javax.ws.rs.DELETE;
        import javax.ws.rs.FormParam;
        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 javax.ws.rs.core.Response;

        import org.glassfish.jersey.media.multipart.FormDataParam;

        import com.glarimy.lib.api.Book;
        import com.glarimy.lib.api.BookDAL;
        import com.glarimy.lib.db.BookDALTrans;
        import com.google.gson.Gson;

        @Path("/lib")
        public class LibraryController 
        {
            private BookDAL bookdal;
            //private String newTitle;
            public LibraryController() 
            {
                bookdal = BookDALTrans.getInstance();
            }

            //Adding a book
            @POST
            @Path("/addbook")
            @Consumes({MediaType.MULTIPART_FORM_DATA})//Accepts only strings
            @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
            public Response create(@FormDataParam("title") String title, @FormDataParam("author") String author, 
                                   @FormDataParam("price") String price, @FormDataParam("pages") String pages)
            {
                System.out.println("hello");
                double price1 = Double.parseDouble(price);
                int pages1=Integer.parseInt(pages);
                Book book=new Book(title,author,price1,pages1);
                bookdal.add(book);
                return Response.ok().entity(book).build();
            }


            //Searching for a book
            @GET
            @Path("/findbook/{isbn}")
            @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
            public Response find(@PathParam("isbn1") int isbn1) 
            {
                Book book=bookdal.find(isbn1);
                return Response.ok().entity(book).build();      
            }


            //Updating a book
            @PUT
            @Path("/updatebook/{isbn}")
            @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
            @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
            public Response update(@PathParam("isbn") int isbn, @FormParam("title") String newTitle, 
                                   @FormParam("author") String author,@FormParam("price") double price, 
                                   @FormParam("noOfPages") int pages)
            {
                Book book=bookdal.update(isbn,newTitle,author,price,pages);
                return Response.ok().entity(book).build();
            }


            //Deleting a book
            @DELETE
            @Path("/deletebook")
            public Response remove(@FormParam("isbn") int isbn) 
            {
                bookdal.delete(isbn);
                return Response.ok().build();
            }
        }

Here is my web.xml file 

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <servlet>
        <servlet-name>jersey</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
          <param-name>jersey.config.server.provider.packages</param-name>
          <param-value>com.glarimy.lib.rs</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>jersey</servlet-name>
        <url-pattern>/glarimy/*</url-pattern>
      </servlet-mapping>
    </web-app>



This is my POM file

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.glarimy.rest</groupId>
    <artifactId>library</artifactId>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>1.19</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-servlet</artifactId>
            <version>1.19</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.19</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.40</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
            <version>2.17</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>mysql</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <outputDirectory>D:\Glarimy\Programs\tomcat\webapps</outputDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <version>4</version>
</project>

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。 我通过替换

更改了POM文件
 <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
            <version>2.17</version>
 </dependency>

with

   <dependency>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-multipart</artifactId>
            <version>1.19</version>
   </dependency>

然后evereything应该很好。