0

I'm trying to develop a simple restful api in java using jersey implementation of Jax-rs and Tomcat 9.XX server. The application produces data in XML format.

But when calling the api resource, I'm getting the following exception from the server:

Type Exception Report

Message: Servlet.init() for servlet [ApplicationConfig] threw exception

Description: The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception:

javax.servlet.ServletException: Servlet.init() for servlet [ApplicationConfig] threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:748)

Root Cause:

java.lang.IllegalStateException: The resource configuration is not modifiable in this context.
    org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:246)
    org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:193)
    org.glassfish.jersey.server.ResourceConfig.register(ResourceConfig.java:426)
    org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:306)
    org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:154)
    org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:346)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:748)

I'm unable to figure out what's causing this issue.

The base model of the application was able to properly output xml data but when I tried to implement HATEOS model for including links in the data, server has started throwing exceptions.

jersey app config:

import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath(value = "restapi")
public class ApplicationConfig extends Application {

    @Override
    public Map<String, Object> getProperties() {
        Map<String, Object> properties = new HashMap<>();
        properties.put("jersey.config.server.provider.packages", "restresources");
        return properties;
    }
}

Rest resource:

@Path(value = "poketype")
public class PokemonTypeRestResource {

    PokemonTypeService typeService = new PokemonTypeService();

    @GET
    @Produces(value = MediaType.APPLICATION_XML)
    public List<PokemonType> returnAllTypes(@Context UriInfo uriInfo) {
        List<PokemonType> allTypes = typeService.getAllTypes();
        allTypes.forEach((t) -> {
            String uri = uriInfo.getBaseUriBuilder().path(PokemonTypeRestResource.class).path(t.getType()).build().toString();
            t.getLinks().add(new Link(uri, "Self"));
        });
        return allTypes;
    }

    @Path("{type}")
    @GET
    @Produces(value = MediaType.APPLICATION_XML)
    public List<PokemonType> returnSpecificType(@PathParam("type") String type, @Context UriInfo uriInfo) {
        List<PokemonType> specificType = typeService.getSpecificType(type);
        specificType.forEach((t) -> {
            String uri = uriInfo.getBaseUriBuilder().path(PokemonTypeRestResource.class).path(t.getType()).build().toString();
            t.getLinks().add(new Link(uri, "Self"));
        });
        return specificType;
    }

    @Path("filter")
    @GET
    @Produces(value = MediaType.APPLICATION_XML)
    public List<PokemonType> returnWeatherBoostedPokemonTypes(@QueryParam("weather") String weather, @Context UriInfo uriInfo) {
        List<PokemonType> weatherBoostedTypes = typeService.getWeatherBoostedTypes(weather);
        weatherBoostedTypes.forEach((t) -> {
            String uri = uriInfo.getBaseUriBuilder().path(PokemonTypeRestResource.class).path(t.getType()).build().toString();
            t.getLinks().add(new Link(uri, "Self"));
        });
        return weatherBoostedTypes;
    }
}

Resource model:

@XmlRootElement(name = "Pokemon_Type")
@XmlAccessorType(value = XmlAccessType.FIELD)
@Entity
@Table(name = "pokemon_types")
public class PokemonType implements Serializable
{
    //type & weather
    @XmlElement(name = "Type")
    @Id
    @Column(name="type")
    private String type;

    @XmlElement(name = "Boosted_Weather")
    @Column(name="weather")
    private String weather;

    @XmlElement(name = "Links")
    private List<Link> links = new ArrayList<>();

    //constructors
    //getters & setters
}

I'm unable to find anything wrong with ApplicationConfig.java class as this worked before without any issues in other applications. Am I messing anything else in configuring the jersey application?

  • Is there a reason you're extending `Application` instead of `ResourceConfig`? – Paul Samsotha Feb 10 '19 at 02:48
  • I was following tutorials regarding 'jersey configuration using annotation' and this was the class they extended in the tutorial. Same code is used in multiple tutorials actually. – ShashiKanth Chill Feb 10 '19 at 05:31
  • 1
    Check [this](https://stackoverflow.com/a/45627178/2587435) out. See if using ResourceConfig works. – Paul Samsotha Feb 10 '19 at 07:24
  • Thank you for linking this. I now understand the difference between 2 classes. I also re-built the project from ground up with the changes (using ResourceConfig this time & declaring XMLElement attributes in Link.java) and it's working fine now. Thanks for helping on this :) – ShashiKanth Chill Feb 10 '19 at 11:55

0 Answers0