java restful项目打包_tomcat 部署 RESTful 服务实例

本文介绍了如何创建一个简单的Java RESTful服务,包括编写代码、配置web.xml,然后详细阐述了三种在Tomcat服务器上部署RESTful服务的方法:直接放置war包、通过管理界面部署和修改server.xml配置文件进行部署。每种方法都附带了详细的步骤和浏览器测试验证。
摘要由CSDN通过智能技术生成

1、建立简单restfule服务

简单代码:

packagecom.example;importjavax.ws.rs.GET;importjavax.ws.rs.Path;importjavax.ws.rs.Produces;importjavax.ws.rs.core.MediaType;/*** Root resource (exposed at "myresource" path)*/@Path("myresource")public classMyResource {/*** Method handling HTTP GET requests. The returned object will be sent

* to the client as "text/plain" media type.

*

*@returnString that will be returned as a text/plain response.*/@GET

@Produces(MediaType.TEXT_PLAIN)//http://localhost:8080/api/myresource/getIt

@Path("/getIt")//localhost:8080/api/myresource/getIt

publicString getIt() {return "Got it!";

}

}

web.xml文件

test

org.glassfish.jersey.servlet.ServletContainer

jersey.config.server.provider.packages

com.example

1

test

/api/*

生成war包:

运行测试成功

浏览器测试:

2、部署服务

有四种方式:

方式1 、最简单,常见的部署方法,直接将war包放到tomcat的wabapp目录下

将war包直接放在webapps下,然后启动tomcat ,在webapps下就生成一个和war包同名的文件夹solr_service_war,实际上就是war包解压的文件

启动tomcat

在浏览器测试成功,主要url上要注意加上solr_service_war

方式2、利用控制台部署

启动tomcat,浏览器输入:http://localhost:8080/manager/html

假如要部署的war包存放在D:\webapp路径下

通过界面输入以下信息

点击"Deploy" 完成部署

同时在tomcat的webapps文件夹下名称有下面两个生成

启动浏览器测试:

也可以直接生成

方式3 、将项目打包到本地随便一个目录位置,然后用tomcat部署该项目

导出项目的war包到本地随便一个文件夹下:我这里新建了一个D:\webapp的文件夹,并将war包放到这里。

同样修改 tomcat下的conf下的server.xml配置文件

和上面不同的是path必须有指定路径,docBase后面是文件,而上面的是文件夹,否则就报错

启动tomcat,就会在webapps文件夹下面生成SolrService文件夹,里面的内容就解压D:\webapp\test_war.war里面的包得到文件

在浏览器测试

restful restful所需要的jar包 ========================================= Restlet, a RESTful Web framework for Java ========================================= http://www.restlet.org ----------------------------------------- Native REST support * Core REST concepts have equivalent Java classes (UniformInterface, Resource, Representation, Connector for example). * Suitable for both client-side and server-side web applications. The innovation is that that it uses the same API, reducing the learning curve and the software footprint. * Restlet-GWT module available, letting you leverage the Restlet API from within any Web browser, without plugins. * Concept of "URIs as UI" supported based on the URI Templates standard. This results in a very flexible yet simple routing with automatic extraction of URI variables into request attributes. * Tunneling service lets browsers issue any HTTP method (PUT, DELETE, MOVE, etc.) through a simple HTTP POST. This service is transparent for Restlet applications. Complete Web Server * Static file serving similar to Apache HTTP Server, with metadata association based on file extensions. * Transparent content negotiation based on client preferences. * Conditional requests automatically supported for resources. * Remote edition of files based on PUT and DELETE methods (aka mini-WebDAV mode). * Decoder service transparently decodes compressed or encoded input representations. This service is transparent for Restlet applications. * Log service writes all accesses to your applications in a standard Web log file. The log format follows the W3C Extended Log File Format and is fully customizable. * Powerful URI based redirection support similar to Apache Rewrite module. Available Connectors * Multiple server HTTP connectors available, based on either Mortbay's Jetty or the Simple framework or Grizzly NIO framework. * AJP server connector available to let you plug behind an Apache HTT
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值