问题描述
使用球衣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
软件包中
您具有必需的依赖项
You have the required dependencies
没有其他未知问题与您未向我们展示的内容有关
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>
应该可以通过http://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:
-
如果已经有一个处理该应用程序的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="http://java.sun.com/xml/ns/javaee"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
4 http://java.sun.com/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服务中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!