没有web.xml的Jersey设置 [英] Jersey setup without web.xml

查看:109
本文介绍了没有web.xml的Jersey设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个使用 Jersey 的简单REST Web应用程序.在文档中,似乎无需使用web.xml文件就可以创建我的应用程序.来自网站:

I'm attempting to set up a simple REST web application that uses Jersey. In the documentation, it seems that I should be able to create my application without using a web.xml file. From the site:

JAX-RS提供了一个与部署无关的抽象类Application,用于声明根资源和提供者类以及根资源和提供者单例实例. Web服务可以扩展此类,以声明根资源和提供程序类.

JAX-RS provides a deployment agnostic abstract class Application for declaring root resource and provider classes, and root resource and provider singleton instances. A Web service may extend this class to declare root resource and provider classes.

下面的示例显示以下代码:

The example that follows shows this code:

public class MyApplication extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> s = new HashSet<Class<?>>();
        s.add(HelloWorldResource.class);
        return s;
    }
}

对我来说,这表示我可以使用Application类来完成我的所有servlet设置.这似乎是读取我的资源类的注释并设置正确的URL处理机制的配置.那是对的吗?我不必进行任何其他设置吗?

To me, this says that I can use an Application class to do all of my servlet setup. This seems to be the configuration that reads my resource class's annotations and sets up the correct URL handling mechanisms. Is that correct? I don't have to do any other setup?

我问是因为我创建了以下内容,但它不起作用(我从localhost:8080/{context}/test处获得了404):

I ask because I created the following and it didn't work (I get a 404 from localhost:8080/{context}/test):

pom.xml:

<dependencies>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>1.12</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.12</version>
    </dependency>

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

    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>jsr311-api</artifactId>
        <version>1.1.1</version>
    </dependency>
</dependencies>

应用程序类:

@ApplicationPath("/")
public class JerseyTestApp extends Application
{
    @Override
    public Set<Class<?>> getClasses()
    {
        final Set<Class<?>> classes = new HashSet<>();
        classes.add(JerseyTestController.class);
        return classes;
    }
}

资源类:

@Path("/test")
public class JerseyTestController 
{
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getTestMsg() 
    {
        return "It works";
    }
}

推荐答案

哑巴.我要做的就是按照此答案的规定包含jersey-servlet jar.

Dumb. All I had to do was include the jersey-servlet jar, as prescribed by this answer.

这篇关于没有web.xml的Jersey设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆