在球衣应用程序部署期间如何修复“ Servlet [ApplicationConfig]抛出异常的Servlet.init()”?

时间:2019-02-09 12:51:33

标签: java rest tomcat jersey jax-rs

我正在尝试使用Jax-rs和Tomcat 9.XX服务器的jersey实现在Java中开发一个简单的Restful API。该应用程序以XML格式生成数据。

但是当调用api资源时,我从服务器收到以下异常:

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)

我无法弄清楚是什么原因导致了此问题。

应用程序的基本模型能够正确输出xml数据,但是当我尝试实现HATEOS模型以在数据中包括链接时,服务器已开始引发异常。

球衣应用配置:

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

其他资源:

@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;
    }
}

资源模型:

@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
}

我无法找到ApplicationConfig.java类的任何错误,因为以前没有其他应用程序中的问题,因此可以正常工作。配置球衣应用程序时,我是否会弄乱其他东西?

0 个答案:

没有答案