当前位置: 首页 > 知识库问答 >
问题:

HTTP状态500-Servlet。servlet示例API的init()在尝试使用Jersey返回JSON对象时引发异常

鲍驰
2023-03-14

我得到以下错误:

HTTP状态500-Servlet。servlet示例API的init()引发异常

类型异常报告

消息Servlet.init()为servlet示例API抛出异常

描述服务器遇到了一个内部错误,使其无法完成此请求。

例外

javax。servlet。ServletException:Servlet。servlet示例API的init()引发了异常组织。阿帕奇。卡塔琳娜。验证者。AuthenticatorBase。调用(AuthenticatorBase.java:502)org。阿帕奇。卡塔琳娜。阀门。错误报告阀。调用(ErrorReportValve.java:79)组织。阿帕奇。卡塔琳娜。阀门。抽象逻辑阀。调用(AbstractAccessLogValve.java:617)org。阿帕奇。卡塔琳娜。连接器。郊狼适应者。服务(CoyoteAdapter.java:518)org。阿帕奇。郊狼。http11。抽象http11处理器。进程(AbstractHttp11Processor.java:1091)组织。阿帕奇。郊狼。AbstractProtocol$AbstractConnectionHandler。进程(AbstractProtocol.java:668)org。阿帕奇。公猫util。网NioEndpoint$SocketProcessor。doRun(NioEndpoint.java:1527)org。阿帕奇。公猫util。网NioEndpoint$SocketProcessor。运行(NioEndpoint.java:1484)java。util。同时发生的线程池执行器。runWorker(ThreadPoolExecutor.java:1142)java。util。同时发生的线程池执行器$Worker。运行(ThreadPoolExecutor.java:617)org。阿帕奇。公猫util。线程。TaskThread$WrappingRunnable。运行(TaskThread.java:61)java。朗。丝线。运行(Thread.java:745)根本原因

JAVAlang.IllegalArgumentException:java。文本ParseException:下一个事件不是令牌com。太阳运动衫果心标题。媒体类型。createQualitySourceMediaTypes(MediaTypes.java:289)com。太阳运动衫果心标题。媒体类型。createQualitySourceMediaTypes(MediaTypes.java:274)com。太阳运动衫服务器impl。modelapi。注释。内省模型。addproducts(IntrospectionModeller.java:173)com。太阳运动衫服务器impl。modelapi。注释。内省模型。WorkUtreSourceMethodList(introspectionModeler.java:303)com。太阳运动衫服务器impl。modelapi。注释。内省模型。createResource(IntrospectionModeller.java:126)com。太阳运动衫服务器impl。应用WebApplicationImpl。getAbstractResource(WebApplicationImpl.java:769)com。太阳运动衫服务器impl。应用WebApplicationImpl。createAbstractResourceModelStructures(WebApplicationImpl.java:1595)com。太阳运动衫服务器impl。应用WebApplicationImpl_启动(WebApplicationImpl.java:1356)com。太阳运动衫服务器impl。应用WebApplicationImpl。访问$700(WebApplicationImpl.java:180)com。太阳运动衫服务器impl。应用WebApplicationImpl$13。f(WebApplicationImpl.java:799)com。太阳运动衫服务器impl。应用WebApplicationImpl$13。f(WebApplicationImpl.java:795)com。太阳运动衫spi。注射错误。processWithErrors(Errors.java:193)com。太阳运动衫服务器impl。应用WebApplicationImpl。启动(WebApplicationImpl.java:795)com。太阳运动衫服务器impl。应用WebApplicationImpl。启动(WebApplicationImpl.java:790)com。太阳运动衫spi。容器servlet。ServletContainer。初始化(ServletContainer.java:509)com。太阳运动衫spi。容器servlet。ServletContainer$InternalWebComponent。启动(ServletContainer.java:339)com。太阳运动衫spi。容器servlet。网络组件。加载(WebComponent.java:605)com。太阳运动衫spi。容器servlet。网络组件。init(WebComponent.java:207)com。太阳运动衫spi。容器servlet。ServletContainer。init(ServletContainer.java:394)com。太阳运动衫spi。容器servlet。ServletContainer。init(ServletContainer.java:577)javax。servlet。GenericServlet。init(GenericServlet.java:158)org。阿帕奇。卡塔琳娜。验证者。AuthenticatorBase。调用(AuthenticatorBase.java:502)org。阿帕奇。卡塔琳娜。阀门。错误报告阀。调用(ErrorReportValve.java:79)组织。阿帕奇。卡塔琳娜。阀门。抽象逻辑阀。调用(AbstractAccessLogValve.java:617)org。阿帕奇。卡塔琳娜。连接器。郊狼适应者。服务(CoyoteAdapter.java:518)org。阿帕奇。郊狼。http11。抽象http11处理器。进程(AbstractHttp11Processor.java:1091)组织。阿帕奇。郊狼。AbstractProtocol$AbstractConnectionHandler。进程(AbstractProtocol.java:668)org。阿帕奇。公猫util。网NioEndpoint$SocketProcessor。doRun(NioEndpoint.java:1527)org。阿帕奇。公猫util。网NioEndpoint$SocketProcessor。运行(NioEndpoint.java:1484)java。util。同时发生的线程池执行器。runWorker(ThreadPoolExecutor.java:1142)java。util。同时发生的线程池执行器$Worker。运行(ThreadPoolExecutor.java:617)org。阿帕奇。公猫util。线程。TaskThread$WrappingRunnable。运行(TaskThread.java:61)java。朗。丝线。运行(Thread.java:745)根本原因

java.text.语法异常:下一个事件不是令牌com.sun.jersey.core.header.reader.HttpHeaderReader.next令牌(HttpHeaderReader.java:102)com.sun.jersey.core.header.QualitySourceMediaType.value(QualitySourceMediaType.java:84)com.sun.jersey.core.header.reader.HttpHeaderReader$5.create(HttpHeaderReader.java:360)com.sun.jersey.core.header.reader.HttpHeaderReader$5.create(HttpHeaderReader.java:358)com.sun.jersey.core.header.reader.HttpHeaderReader.read列表(HttpHeaderReader.java:481)com.sun.jersey.core.header.reader.HttpHeaderReader.read列表(HttpHeaderReader.java:473)com.sun.jersey.core.header.reader.HttpHeaderReader. readAcceptableList(HttpHeaderReader. java: 461)com. sun. jersey. core. head. reader.HttpHeaderReader. readQualitySourceMediaType(HttpHeaderReader. java: 365)com. sun. jersey. core. head. reader.HttpHeaderReader. readQualitySourceMediaType(HttpHeaderReader. java: 373)com. sun. jersey. core. head.MediaType. createQualitySourceMediaType(MediaType. java: 287)com. sun. jersey. core. Header.MediaType. createQualitySourceMediaTypes(MediaType. java: 274)com. sun. jersey. server. implified. Modelapi.注释。addProduces(AutomSpectionModeller. java: 173)com. sun. jersey. server. implified. Modelapi.注释。java: 303)com. sun. jersey. server. imp. Modelapi.注释。CreateResources(介绍Modeller. java: 126)com. sun. jersey. server. imp. Application.java: 769)com. sun. jersey. server. implified. Application.WebApplication. createAbstractResourceModel结构(WebApplication. Inv. java: 1595)com. sun. jersey. server. imp. Application._initiate(WebApplication ationInpl. java: 1356)com. sun. jersey. server. imp. Application.网络应用程序应用程序。访问700美元(网络应用程序应用程序。java: 180)com. sun. jersey. server. implified.应用程序。Web应用程序应用13美元. f(Web应用程序应用程序. java: 799)com. sun. jersey. server. implified. Application.网络应用程序13美元。f(网络应用程序。java: 795)com. sun. jersey. spi. inject。Errs. ProcAnd错误(Errs. java: 193)com. sun. jersey. server. imp. Application.启动(WebApplication. Inv. java: 795)com. sun. jersey. server. Inp. Application.启动(WebApplication. Inv. java: 790)com. sun. jersey. spi.容器. servlet。服务器容器。启动(ServletContainer. java: 509)com. sun. jersey. spier. servlet。ServletContainer$InternalWebComponent. start(ServletContainer. java: 339)com. sun. jersey. spi.容器. servlet.加载(WebComponent. java: 605)com. sun. jersey. spi.容器. servlet。init(WebComponent. java: 207)com. sun. jersey. spi.容器. servlet.ServletContainer. init(ServletContainer. java: 394)com. sun. jersey. spier. servlet.init(ServletContainer. java: 577)javax. servlet.GenericServlet. init(GenericServlet. java: 158)org. apache. catalina.验证器。其一:函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数。ErrReportValve. invoke(ErrReportValve. java: 79)org. apache. catalina. valves。AbstractAccessLogValve. invoke(AbstractAccessLogValve. java: 617)org. apache. catalina.连接器。CoyoteAdapter. service(CoyoteAdapter. java: 518)org. apache. coyot. http 11.进程(AbstractHttp11Processor. java: 1091)org. apache. coyote.进程(AbstractAction. java: 668)org. apache. tomcat. util. net。NioEndpoint$SocketProcessor. doRun(NioEndpoint. java: 1527)org. apache. tomcat. util. net。NioEndpoint$SocketProcessor. run(NioEndpoint. java: 1484)java. util.并发。ThreadPoolExecutor. runWorker(ThreadPoolExecutor. java: 1142)java. util. con当前。线程池执行器$Worker. run(ThreadPoolExecutor. java: 617)org. apache. tomcat. util. threads。TaskThread$WrappingRunnable. run(TaskThread. java: 61)java. lang.Thread. run(Thread. java: 745)注意根本原因的完整堆栈跟踪在Apache Tomcat/8.0.24日志中可用。

ApacheTomcat/8.0.24

当我尝试运行以下程序时:

package com.example.api;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlRootElement;

import org.json.JSONObject;

@XmlRootElement
class User
{
    String user;

    public User () {}

    public User (String user)
    {
        this.user = user;
    }

    public String getUser ()
    {
        return this.user;
    }

    public void setUser (String user)
    {
        this.user = user;
    }
}

@Path("/hello/{user}")
public class Sample {
    @GET
    @Produces("/application/json")
    public User getMessage(@PathParam("user") String user) {

        JSONObject j = new JSONObject(new User (user));
        return new User (user);
      //  System.out.println(j.toString());
       // return j.toString();
    }
}

以下是网站。xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">


    <servlet>
        <servlet-name>Example API</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.example.api</param-value>
        </init-param>

        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>Example API</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

这是pom。xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<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.example.mock</groupId>
    <artifactId>MockAPI</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>


    <dependencies>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-bundle</artifactId>
            <version>1.18.3</version>
        </dependency>


        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20141113</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>1.18</version>
        </dependency>

    </dependencies>



    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <webXml>src\main\webapp\WEB-INF\web.xml</webXml>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

如果生成纯字符串,程序运行正常。为什么我可以在这里返回JSON对象?

谢谢

共有1个答案

陶锋
2023-03-14

您的products注释应该是@products(“application/json”)只需删除前导的斜杠即可。您还可以使用MediaType。应用程序\u JSON

 类似资料: