Jersey setup without web.xml

安稳与你 提交于 2019-12-06 02:01:27

问题


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

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?


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>

Application class:

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

Resource class:

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

回答1:


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



来源:https://stackoverflow.com/questions/17816561/jersey-setup-without-web-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!