0
votes

I get the following errors:

HTTP Status 500 - Servlet.init() for servlet Example API threw exception

type Exception report

message Servlet.init() for servlet Example API threw exception

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet.init() for servlet Example API threw exception org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.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.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(Thread.java:745) root cause

java.lang.IllegalArgumentException: java.text.ParseException: Next event is not a Token com.sun.jersey.core.header.MediaTypes.createQualitySourceMediaTypes(MediaTypes.java:289) com.sun.jersey.core.header.MediaTypes.createQualitySourceMediaTypes(MediaTypes.java:274) com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller.addProduces(IntrospectionModeller.java:173) com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller.workOutResourceMethodsList(IntrospectionModeller.java:303) com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller.createResource(IntrospectionModeller.java:126) com.sun.jersey.server.impl.application.WebApplicationImpl.getAbstractResource(WebApplicationImpl.java:769) com.sun.jersey.server.impl.application.WebApplicationImpl.createAbstractResourceModelStructures(WebApplicationImpl.java:1595) com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1356) com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:180) com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:799) com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:795) com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193) 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:502) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.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.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(Thread.java:745) root cause

java.text.ParseException: Next event is not a Token com.sun.jersey.core.header.reader.HttpHeaderReader.nextToken(HttpHeaderReader.java:102) com.sun.jersey.core.header.QualitySourceMediaType.valueOf(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.readList(HttpHeaderReader.java:481) com.sun.jersey.core.header.reader.HttpHeaderReader.readList(HttpHeaderReader.java:473) com.sun.jersey.core.header.reader.HttpHeaderReader.readAcceptableList(HttpHeaderReader.java:461) com.sun.jersey.core.header.reader.HttpHeaderReader.readQualitySourceMediaType(HttpHeaderReader.java:365) com.sun.jersey.core.header.reader.HttpHeaderReader.readQualitySourceMediaType(HttpHeaderReader.java:373) com.sun.jersey.core.header.MediaTypes.createQualitySourceMediaTypes(MediaTypes.java:287) com.sun.jersey.core.header.MediaTypes.createQualitySourceMediaTypes(MediaTypes.java:274) com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller.addProduces(IntrospectionModeller.java:173) com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller.workOutResourceMethodsList(IntrospectionModeller.java:303) com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller.createResource(IntrospectionModeller.java:126) com.sun.jersey.server.impl.application.WebApplicationImpl.getAbstractResource(WebApplicationImpl.java:769) com.sun.jersey.server.impl.application.WebApplicationImpl.createAbstractResourceModelStructures(WebApplicationImpl.java:1595) com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1356) com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:180) com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:799) com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:795) com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193) 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:502) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.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.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(Thread.java:745) note The full stack trace of the root cause is available in the Apache Tomcat/8.0.24 logs.

Apache Tomcat/8.0.24

When I try to run the following the following program:

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

Following is the web.xml file:

<?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>

This is the pom.xml file:

<?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>

If I produce plain string, the program runs fine. Why can I return JSON objects here?

Thanks.

1

1 Answers

2
votes

Your Produces annotation should be @Produces("application/json") just remove the leading slash. You can also use MediaType.APPLICATION_JSON