gpt4 book ai didi

java - 尝试使用 Postman to REST 进行 POST 时出现异常

转载 作者:太空宇宙 更新时间:2023-11-04 12:04:58 24 4
gpt4 key购买 nike

在这里,我尝试使用 postman 向“Rest 应用程序”提供“表单数据”,但我遇到了下面的异常。

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>

最佳答案

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

更改了 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>

然后一切都进展顺利。

关于java - 尝试使用 Postman to REST 进行 POST 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40466008/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com