json Jersey 添加 jersey-* 依赖项时不存在 WebApplication 提供程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8662919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:07:31  来源:igfitidea点击:

Jersey No WebApplication provider is present when jersey-* dependency added

jsonspringjersey

提问by idbentley

I have a simple Spring & Jersey application, which works perfectly well for consuming requests through a simple Resource. However, I'd like to return a JSON response - containing a simple JSON serialization of an object. To achieve this, I've added a maven dependency for jersey-json. As soon as I add this dependency, however, I get this error at server startup:

我有一个简单的 Spring & Jersey 应用程序,它非常适合通过简单的资源来使用请求。但是,我想返回一个 JSON 响应 - 包含一个对象的简单 JSON 序列化。为了实现这一点,我为jersey-json. 但是,一旦我添加了此依赖项,就会在服务器启动时收到此错误:

com.sun.jersey.api.container.ContainerException: No WebApplication provider is present  at      
     com.sun.jersey.spi.container.WebApplicationFactory.createWebApplication(WebApplicationFactory.java:69) at
     com.sun.jersey.spi.container.servlet.ServletContainer.create(ServletContainer.java:391)

I'm not totally clear upon exactly what a provider is, but I'm pretty certain that there should be a default one found.

我不完全清楚提供者到底是什么,但我很确定应该找到一个默认的。

For completeness, here's my Resource:

为了完整起见,这是我的资源:

@Path("/scan")
@Resource
@Component
public class ScanResource {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/{barcode}")
    public List<Scan> getScansForBarcode(@PathParam("barcode") Long barcode){
        ..snip..
        return results;
    }
}

A Scan object is a simple Entity Bean object.

Scan 对象是一个简单的实体 Bean 对象。

The mvn dependency is:

mvn 依赖项是:

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

Does anyone know why I might be getting the No WebApplication provider is presentException? Any thoughts on how I might resolve it?

有谁知道为什么我可能会收到No WebApplication provider is present异常?关于我如何解决它的任何想法?

Thanks

谢谢

回答by Pavel Bucek

You need to have jersey-server jar on your classpath as well. And you need to make sure that all your jars are from the same version, Jersey runtime won't be able to use provided classes otherwise.

您还需要在类路径上安装 jersey-server jar。并且您需要确保所有 jar 都来自同一版本,否则 Jersey 运行时将无法使用提供的类。

Additionally (most likely not relevant here, but..) there is a recent change in module structure - servlet dependencies were separated to new modules. So if you are using servlets, you might want to depend on jersey-servlet (which depends on jersey-server).

此外(这里很可能不相关,但是..)模块结构最近发生了变化 - servlet 依赖项被分离到新模块。因此,如果您使用 servlet,您可能希望依赖 jersey-servlet(这取决于 jersey-server)。

回答by krishbhagya

I am also had this issue. The issue was resolved by having same version for "jersey-json" and "jersey-servlet"maven dependencies.

我也有这个问题。该问题已通过对“jersey-json”和“jersey-servlet”maven 依赖项使用相同的版本来解决。

EX:

前任:

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