使用JAX-RS创建RESTful Web服务并将其部署到tomcat [英] Create RESTful Web Service with JAX-RS and deploy it to tomcat

查看:59
本文介绍了使用JAX-RS创建RESTful Web服务并将其部署到tomcat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JAX-RS创建和部署RESTful Web服务并将其部署到tomcat.我不想使用任何IDE.在Tomcat中,webapps中具有以下目录结构\

I'm trying to create and deploy a RESTful Web Service using JAX-RS and deploy it to tomcat. I don't want to use any IDE. In Tomcat I have the following directory structure inside webapps\

notifire\WEB-INF\
                 |
                 ---> web.xml
                 |
                 ---> \classes/Notifier.class
                 |
                 ---> \lib\javax.ws.rs-api-2.0

我的web.xml包含:

my web.xml contains:

<servlet>
<servlet-name>Web Service Servlet</servlet-name>
<servlet-class>javax.ws.rs.core.Application</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Web Service Servlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>

和类文件 Notifier.class 是从文件 Notifier.java 编译而来的.

and the class file Notifier.class was compiled from the file Notifier.java.

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;

@Path("notifier")
public class Notifier {

    @Context
    private UriInfo context;

    @GET
    @Produces("text/html")
    public String getHTML() {

         return "<p></p>";
    }
}

当我尝试通过 http://localhost:8080/notifire/webservice/notifier 访问Web服务时,出现以下错误:

When I try to access the Web Service at http://localhost:8080/notifire/webservice/notifier I get the following error:

-类型例外报告

-消息类javax.ws.rs.core.Application不是Servlet

--message Class javax.ws.rs.core.Application is not a Servlet

-说明服务器遇到内部错误,导致服务器无法满足此请求.

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

感谢您的帮助.

推荐答案

您的Servlet类错误.不确定为什么不想使用IDE,但是有一个Maven原型可以使用Jersey开发人员定义的适当类为您布置项目结构.我的web.xml看起来像这样:

You have the wrong class for your Servlet. Not sure why you are not wanting to use an IDE, but there is a maven archetype that will layout your project structure for you using the appropriate classes that the Jersey developers have defined. My web.xml looks like this:

<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.pluralsight</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/webapi/*</url-pattern>
</servlet-mapping>

我在本课程的此处.

这篇关于使用JAX-RS创建RESTful Web服务并将其部署到tomcat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆