简体   繁体   English

如何正确配置jax-rs web服务web.xml

[英]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.

相关问题 基于注释的Spring / JAX-RS集成,没有web.xml - Annotation based Spring / JAX-RS integration with no web.xml 在没有Web.xml的JAX-RS上使用Shiro筛选器 - Using a Shiro Filter on JAX-RS without Web.xml 带有 Tomcat 的简单 JAX-RS - 404 Not Found(没有 web.xml) - Simple JAX-RS with Tomcat - 404 Not Found (No web.xml) 如何在JAX-RS Web服务中返回XML中的响应? - How to return Response in XML in JAX-RS web service? 如何仅使用注释(无 web.xml)设置 JAX-RS 应用程序? - How to set up JAX-RS Application using annotations only (no web.xml)? 如何通过REST(JAX-RS)在web.xml中声明多个包? - How to declare multiple packagesin web.xml, by REST (JAX-RS)? 如何在没有 web.xml 的情况下使用 Jersey 作为 JAX-RS 实现? - How to use Jersey as JAX-RS implementation without web.xml? 如何通过JAX-RS应用程序中的注释从基于web.xml的授权切换为授权 - How to switch from web.xml based authorization to authorization via annotations in a JAX-RS application 我什么时候需要在web.xml中包含Jersey ServletContainer以启用JAX-RS? - When do I need to include Jersey ServletContainer in web.xml to enable JAX-RS? 使用JAX-RS更改服务器设置新的Configuartion“应用程序”类(没有web.xml)? - Change Server Settings with JAX-RS New Configuartion “Application” class (without web.xml)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM