你应该在哪里放置在jax.rs中扩展Application的类

时间:2015-02-12 06:01:23

标签: java web-services rest maven

下面的代码是我在netbeans中创建的新项目,但我使用MAVEN作为项目类型。因为我想创建一个像下面的文件夹结构

+ com.kixortillan.mavenproject1
---+com.kixortillan.mavenproject1.controllers
---+com.kixortillan.mavenproject1.models

我不知道在哪里放置扩展Application的类。我尝试将它放在com.kixortillan.mavenproject1和com.kixortillan.mavenproject1.controllers中的测试控制器中,但它显示错误404

我打电话的网址是http://localhost:8080/mavenproject1/services/test

ApplicationConfig Class

package com.kixortillan.mavenproject1;

import java.util.Set;
import java.util.HashSet;
import javax.ws.rs.core.Application;

@javax.ws.rs.ApplicationPath("services")
public class ApplicationConfig extends Application
{
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> s = new HashSet<Class<?>>();
        s.add(com.kixortillan.mavenproject1.controllers.TestResource.class);
        return s;
    }
}

测试控制器

package com.kixortillan.mavenproject1.controllers;

import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;

/**
 * REST Web Service
 *
 * @author NOC
 */
@Path("test")
public class TestResource
{

    @Context
    private UriInfo context;

    /**
     * Creates a new instance of TestResource
     */
    public TestResource()
    {
    }

    /**
     * Retrieves representation of an instance of com.kixortillan.mavenproject1.controllers.TestResource
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("application/xml")
    public String getXml()
    {
        //TODO return proper representation object
        throw new UnsupportedOperationException();
    }

    /**
     * PUT method for updating or creating an instance of TestResource
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("application/xml")
    public void putXml(String content)
    {
    }
}

非常感谢你能给我一些关于如何在jax.rs中运行的详细信息。

1 个答案:

答案 0 :(得分:0)

您需要确保在pom.xml中有此功能

<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
    </dependency>