[英]How to configure jax-rs web service web.xml properly
I am trying to implement a jax-rs web service using jersey framework. 我正在尝试使用jersey框架实现jax-rs Web服务。 I have written the web service but I don't fully understand what the web.xml tags mean so I don't know if I have configured it correct but when I try to access the service I get an error.
我已经编写了Web服务,但我不完全了解web.xml标签的含义,所以我不知道我是否已将其配置正确,但当我尝试访问该服务时出现错误。 Here is the web service:
这是Web服务:
package org.LMS.Controller;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path ("/test")
public class Test {
private String name = "Worked";
@GET
@Produces (MediaType.APPLICATION_XHTML_XML)
public String getTest ()
{
return name;
}
}
my web.xml is: 我的web.xml是:
<!-- Test web service mapping -->
<servlet>
<display-name>Test</display-name>
<servlet-name>Test</servlet-name>
<servlet-class>org.LMS.Controller</servlet-class>
<init-param>
<param-name>org.LMS.Controller.Test</param-name>
<param-value>eduscope</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
<!--end Test web service mapping -->
and this is the error I'm getting when I try to access my application: HTTP Status 500 - type Exception report message 这是我尝试访问我的应用程序时遇到的错误:HTTP状态500 - 类型异常报告消息
description The server encountered an internal error () that prevented it from fulfilling this request. description服务器遇到内部错误(),导致无法完成此请求。
exception 例外
javax.servlet.ServletException: Wrapper cannot find servlet class org.LMS.Controller or a class it depends on
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
java.lang.Thread.run(Thread.java:679)
root cause
java.lang.ClassNotFoundException: org.LMS.Controller
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
java.lang.Thread.run(Thread.java:679)
Can you guys tell me what I'm doing wrong and explain what each tag in the web.xml file means has it relates to web services 你能告诉我我做错了什么,并解释web.xml文件中的每个标记与web服务有什么关系
You've set the wrong servlet. 你设置了错误的servlet。 Assuming that you're using Jersey , You need to specify your servlet as follows:
假设您正在使用Jersey ,您需要按如下方式指定您的servlet:
<servlet>
<servlet-name>Rest</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>org.LMS.Controller.Test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Rest</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
And when you want to access it, you use the following url 当您想要访问它时,您使用以下URL
http://(host)[:port]/(context path)/rest/test
e.g.
http://localhost:8080/MyRestProject/rest/test
To configure jax-rs webservice using jersey, you can configure with most simple and with only 2 configurations on web.xml (using more steps at java code and annotations), follow steps: 要使用jersey配置jax-rs webservice,您可以在web.xml上配置最简单且只有2个配置(在java代码和注释中使用更多步骤),请按照以下步骤操作:
1) Write a Application (Java code): 1)编写应用程序(Java代码):
package your.package.example;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
public class ExampleApplication extends Application {
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
// Annotated @Path endpoint
s.add(ExampleWebServiceRestClass.class);
return s;
}
}
2) Add config reference (to Application code) on your web.xml: 2)在web.xml上添加配置引用(到应用程序代码):
<web-app>
<servlet>
<servlet-name>your.package.example.ExampleApplication
</servlet-name>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>your.package.example.ExampleApplication
</servlet-name>
<url-pattern>/wspath/*</url-pattern>
</servlet-mapping>
</web-app>
When ever we are configure REST web service with rest at that time, 当我们在那时配置REST Web服务并休息时,
Need to set init-param and init-value for scanning web service class implementation like below Jersey Class for scanning :- 需要设置init-param和init-value来扫描Web服务类实现,如下面的Jersey Class进行扫描: -
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>yourpackegeName</param-value>
</init-param>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.