3

我正在开发项目以升级我现有的 Web 应用程序,该应用程序是在 JAX-RS 1.12 中开发并在 tomcat 7 中运行的。现在我正在将其升级到 JAX-RS2.0。在 tomcat 服务器启动期间,我的资源没有被加载?

以下是详细信息。在 JAX-RS 2.0 的 jar 下添加

jersey-client-2.0-m07-1 jersey-common-2.0-m07-1 jersey-container-servlet-2.0-m07-1 jersey-container-servlet-core-2.0-m07-1 jersey-server-2.0-m07- 1 javax.ws.rs-api-2.0-m10 osgi-resource-locator-1.0.1 javax.inject-2.1.28 javax.inject-1 hk2-utils-2.1.28 hk2-locator-2.1.28 hk2-api -2.1.28 guava-13.0 cglib-2.1.28 asm-all-repackaged-2.1.28

在 Web.xml 中

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>ConfigLiteJersey2</display-name>

<!-- Jersey Servlet to Support JAXRS Services -->
<servlet>
    <servlet-name>ConfigLiteServices</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>        
     <init-param>
        <param-name>javax.ws.rs.core.Application</param-name>
        <param-value>com.cisco.config.resource</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
     <servlet-mapping>
    <servlet-name>ConfigLiteServices</servlet-name>
    <url-pattern>/config/*</url-pattern>
</servlet-mapping>

我的资源文件

@Path("/configset")
public class ConfigSetResource {   
    @POST
@Path("/id{configsetid: (/[^/]+?)?}")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public ConfigSetResponse getConfigSet(@PathParam("configsetid") String sConfigSetId)    throws    Exception {
     //Code
    }
    }

尝试使用以下 URL ipaddress:8080/ConfigLiteJersey2/config/configset/id 访问我的资源 API

获取 HTTP 状态 404 未找到。

看起来我没有在 web.xml 中给出正确的 servletclass 映射。请分享您对此的看法

4

4 回答 4

2

如果您希望 Jersey 扫描您的包以获取资源,请将您的 param-name 更改为:

<param-name>jersey.config.server.provider.packages</param-name>
于 2013-05-02T05:56:27.060 回答
1

我使用的是 Jersey 2.15,下面是工作配置:

<servlet>
    <servlet-name>emper</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.le.config.ResourceConfiguration</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.le.exceptions</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>emper</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet>  

我知道的一件好事是,我们可以注册提供程序类,如Global Exception handler。因此com.le.exceptions包含我的提供者类,它实现ExceptionMapper了泽西岛提供的。

另一件事,我们需要创建一个子类org.glassfish.jersey.server.ResourceConfig并将其注册为我们的jax-rs应用程序。

于 2015-02-17T05:06:05.163 回答
0

If you are using Servlet version 3.0 I would suggest following the example in the jersey manual here: https://jersey.java.net/documentation/latest/deployment.html#deployment.servlet.3.descriptor

Create a class that implements javax.ws.rs.core.Application, say org.foo.rest.ConfigLiteApplication. Then make your web.xml like the following (adapted from the jersey page slightly to match your example):

<web-app>
    <servlet>
        <servlet-name>org.foo.rest.ConfigLiteApplication</servlet-name>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>org.foo.rest.ConfigLiteApplication</servlet-name>
        <url-pattern>/config/*</url-pattern>
    </servlet-mapping>
</web-app>

This should work on both JAX-RS 1.1 and JAX-RS 2.0, and should be neutral to the jersey version as you never need to name any jersey classes. The ConfigLiteApplication class should load the resources you want to include, for example adapted from http://tomee.apache.org/examples-trunk/rest-example-with-application/README.html:

@ApplicationPath("/config")
public class ConfigLiteApplication extends Application {
    public Set<Class<?>> getClasses() {
        return new HashSet<Class<?>>(Arrays.asList(ConfigSetResource.class));
    }
}

A modern IDE will likely be able to manage this class for you.

Also, if I were you I would be careful to check this regular expression match if you are hoping to match on /id

@Path("/id{configsetid: (/[^/]+?)?}")

I would consider splitting this up into multiple functions or otherwise working to try and avoid this kind of regex. For example

@POST
@Path("/id")
public ConfigSetResponse getConfigSet() {
     return this.getConfigSet(null);
}
@POST
@Path("/id/{configsetid}")
public ConfigSetResponse getConfigSet(@PathParam("configsetid") String sConfigSetId) {
     //Code
}
于 2014-10-28T07:40:21.310 回答
0

经过长时间的谷歌搜索,我以这种方式配置了我的,它工作得很好

build.gradle 应该是这样的

compile 'log4j:log4j:1.2.7'
compile 'org.slf4j:slf4j-log4j12:1.6.6'
compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.6'

ApplicationConfig.java 文件应该是这样的

@ApplicationPath("/app")
public class ApplicationConfig extends ResourceConfig {

    public ApplicationConfig() {
        packages("com.flexisaf.resources");

    }
}

你的 web.xml 文件应该是这样的

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <!-- Default page to serve -->

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <display-name>SAFHRMS</display-name>
    <servlet>
        <servlet-name>com.flexisaf.safhrms.client.config.ApplicationConfig</servlet-name>

        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>com.flexisaf.safhrms.client.config.ApplicationConfig</servlet-name>
        <url-pattern>/app/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>safhrms.jsp</welcome-file>
    </welcome-file-list>

</web-app>

这解决了我的问题..谢谢

于 2015-07-02T13:02:38.903 回答