java 如何正确配置jax-rs web service web.xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12452064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:56:47  来源:igfitidea点击:

How to configure jax-rs web service web.xml properly

javaweb-applicationsservletsjax-rs

提问by Mario Dennis

I am trying to implement a jax-rs web service using jersey framework. 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. Here is the web service:

我正在尝试使用 jersey 框架实现 jax-rs Web 服务。我已经编写了 Web 服务,但我不完全理解 web.xml 标签的含义,所以我不知道我是否正确配置了它,但是当我尝试访问该服务时出现错误。这是网络服务:

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 Status 500 - type Exception report message

description The server encountered an internal error () that prevented it from fulfilling this request.

说明 服务器遇到内部错误 (),阻止它完成此请求。

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 文件中的每个标签意味着它与网络服务相关吗

回答by tanyehzheng

You've set the wrong servlet. Assuming that you're using Jersey, You need to specify your servlet as follows:

您设置了错误的 servlet。假设您正在使用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

当您想访问它时,请使用以下网址

http://(host)[:port]/(context path)/rest/test
e.g. 
http://localhost:8080/MyRestProject/rest/test

回答by Isaias Alves

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>

回答by Krunal Patel

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 类:-

<init-param>
   <param-name>com.sun.jersey.config.property.packages</param-name>
   <param-value>yourpackegeName</param-value>
</init-param>