使用Jersey的JAXRS Rest服务中的问题

编程入门 行业动态 更新时间:2024-10-19 21:30:20
本文介绍了使用Jersey的JAXRS Rest服务中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用球衣JAXRS API在tomcat 7上部署了一个演示Rest服务示例.开发了资源类

Deployed a demo Rest Service on tomcat 7 using jersey JAXRS API . Developed a resource class

@Path("/supportdata") public class SupportDataService { public SupportDataService() { // TODO Auto-generated constructor stub } @GET @Produces(MediaType.APPLICATION_XML) public String getSupportData(){ String xmlSupport=null; xmlSupport="<SupportData><Support><key>path1</key><value>value1</value></Support><Support><key>path2</key><value>value2</value></Support></SupportData>"; return xmlSupport; } }

应用程序的子类

public class RestApplication extends Application { public RestApplication() { // TODO Auto-generated constructor stub } @Override public Set<Class<?>> getClasses() { // TODO Auto-generated method stub Set<Class<?>> s=new HashSet<Class<?>>(); s.add(SupportDataService.class); return s; } } Web.xml contains <web-app> <servlet> <servlet-name>com.jaxrs.RestApplication</servlet-name> </servlet> <servlet-mapping> <servlet-name>com.jaxrs.RestApplication</servlet-name> <url-pattern>/resources</url-pattern> </servlet-mapping> </web-app> Included javax.ws.rs-api-2.0.1.jar from jersey in the project. Deployed it on defaultport of tomcat 7 and calling the url on local host port 8080 and url

/RestService/resources/supportdata

/RestService/resources/supportdata

获取404错误的资源不可用.

getting a 404 error of resource not available.

推荐答案

给出...

  • RestApplication在com.jaxrs软件包中

    您具有必需的依赖项 [1]

    You have the required dependencies [1]

    没有其他未知问题与您未向我们展示的内容有关

    No other unknown issues related to something you're not showing us

    您需要做的所有工作才能使它起作用...

    正在改变

    All you need to do to get this to work...

    is change

    <url-pattern>/resources</url-pattern>

    <url-pattern>/resources/*</url-pattern>

    应该可以通过localhost:8080/yourapp/resources/supportdata

    对此进行了测试,效果很好. /resources将网址格式严格限制为/resources.添加/*时,您会说带有/resources前缀的任何内容.

    Tested this and it works fine. /resources is limiting the url pattern to strictly /resources. When you add the /* you are saying anything with the /resources prefix.

    [1] :

    使用Maven,这是我使用的唯一依赖项

    Using Maven, this is the only dependency I used

    <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>2.13</version> </dependency>

    不使用Maven(啊,为什么?):

    Not using Maven (ahhhh, whyyy?):

    我建议您从此处下载 RI包,并将所有jar包含到您的项目中.

    I suggest you download the RI Bundle from here and include all the jars into your project.

    只是为了让其他人为OP的配置scratch之以鼻,这来自JAX-RS规范:

    And just for others scratching their head about the OP's configuration, this is from the JAX-RS spec:

    如果存在Application子类:

    • 如果已经有一个处理该应用程序的servlet.也就是说,一个Servlet的初始化参数为

    • If there is already a servlet that handles this application. That is, a servlet that has an initialization parameter named

    javax.ws.rs.core.Application

    javax.ws.rs.core.Application

    其值是Application子类的全限定名,那么JAX-RS实现无需任何其他配置步骤.

    whose value is the fully qualified name of the Application subclass, then no additional configuration steps are required by the JAX-RS implementation.

    如果没有servlet处理此应用程序,则需要JAX-RS实现来动态添加其完全限定名称必须是Application子类名称的servlet.如果Application子类用@ApplicationPath注释,则要求实现使用此注释的值附加"/*"来定义所添加服务器的映射.否则,必须将应用程序与指定servlet映射的web.xml打包在一起.例如,如果org.example.MyApplication是Application子类的名称,则示例web.xml将是:

    If no servlet handles this application, JAX-RS implementations are REQUIRED to dynamically add a servlet whose fully qualified name must be that of the Application subclass. If the Application subclass is annotated with @ApplicationPath, implementations are REQUIRED to use the value of this annotation appended with "/*" to define a mapping for the added server. Otherwise, the application MUST be packaged with a web.xml that specifies a servlet mapping. For example, if org.example.MyApplication is the name of the Application subclass, a sample web.xml would be:

    1 <web-app version="3.0" xmlns="java.sun/xml/ns/javaee" 2 xmlns:xsi="www.w3/2001/XMLSchema-instance" 3 xsi:schemaLocation="java.sun/xml/ns/javaee 4 java.sun/xml/ns/javaee/web-app_3_0.xsd"> 5 <servlet> 6 <servlet-name>org.example.MyApplication</servlet-name> 7 </servlet> 8 <servlet-mapping> 9 <servlet-name>org.example.MyApplication</servlet-name> 10 <url-pattern>/myresources/*</url-pattern> 11 </servlet-mapping> 12 </web-app>
  • 更多推荐

    使用Jersey的JAXRS Rest服务中的问题

    本文发布于:2023-10-31 06:13:51,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1545254.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:Jersey   JAXRS   Rest

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!