RESTful Jersey多个资源和包

时间:2017-02-02 01:37:30

标签: jersey restful-architecture

我有一个使用Jersey框架和Netbean SDK的简单项目RESTful API。

我创建了一个资源并将其放在除默认值之外的其他包中,并在XML文件中指定了包: The resource is CustomerProfile, and package is Profile. Package "Profile" is not part of the default package 这是我的XML:

<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>trial1.jerseytutorial</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>Profile</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/webapi/*</url-pattern>
</servlet-mapping>

我还在CustomerProfile.java中定义资源

package Profile;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/CustomerProfile")        // the URL path leading to the resource
public class CustomerProfile {
@GET            // map the method to HTTP Get 
@Produces(MediaType.TEXT_PLAIN)   // tell the return format of the request
    public String getProfile() {
        return "Got the profile !";
    }
}

然而,当我导航到http://localhost:8080/JerseyTutorial/webapi/CustomerProfile时,我收到404错误。我怀疑我的XML servlet不对。

1 个答案:

答案 0 :(得分:0)

使用逗号(或分号)分隔符

<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>
        trial1.jerseytutorial,
        Profile
    </param-value>
</init-param>

Haven没有尝试过,但我认为即使是新行分离(不需要逗号或分号)也行不通。