rest helloworld
来源:互联网 发布:胆码计算软件 编辑:程序博客网 时间:2024/04/29 05:37
RESTful WS using Jersey: "Hello world!"
JAX-RS is a java specification which define APIs to implement RESTful web services. Jersey is a reference implementation of JAX-RS. It provides libraries to develop RESTful web services based on the JAX-RS specification. It uses a servlet to receive and dispatch client requests to right execution classes. In this tutorial, we will develop a simple service which returns string “Hello world!” to client using Jersey.
Prerequisites
- Eclipse Java EE.
- Tomcat server.
- Jersey library.
Step 1: setting up Tomcat on Eclipse
- Refer to this tutorial.
Step 2: Create your project
- Open Eclipse, File→New→Dynamic Web Project.
- Name the project: rest. Select Target runtime as your Apache Tomcat. Click Finish.
- Download and extract the Jersey package.
- Copy jersey-server.jar , jersey-core.jar, jersey-servlet.jar and asm.jar to the WebContent→WEB-INF→lib folder.
- Create new package in the src folder. Name it: my.first.rest.
- Create new Java class inside this package. Name it: Hello.
- Edit it as following:
package my.first.rest; import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.Produces;import javax.ws.rs.core.MediaType; @Path("hello")public class Hello { // This method is called if TEXT_PLAIN is requested @GET @Produces(MediaType.TEXT_PLAIN) public String sayHelloInPlainText() { return "Hello world!"; } // This method is called if HTML is requested @GET @Produces(MediaType.TEXT_HTML) public String sayHelloInHtml() { return "<html> " + "<title>" + "Hello world!" + "</title>" + "<body><h1>" + "Hello world!" + "</body></h1>" + "</html> "; }}
Step 3: Define the servlet container
- Create a file: web.xml in the WebContent→WEB-INF folder.
- Insert the following code into the web.xml file:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>rest</display-name> <servlet> <servlet-name>Jersey REST Service</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>my.first.rest</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey REST Service</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
- Note that:
- <param-value> is your package's name.
- the URL of your service will be: http://your_domain:port/<project-name>/<url-pattern>/<path_from_rest_class>. In this example, this URL is: http://localhost:8080/rest/hello (<project-name> is rest, <url-pattern> is empty, <path_from_rest_class> is hello as declared in step 2).
Step 4: Publish and check your service
- Start the Tomcat server
- Right click on Tomcat server→Add and Remove…. Select the rest project.
- Verify the response in HTML by your browser: http://localhost:8080/rest/hello.
- Verify the response in PLAINTEXT using command curl: open your terminal and type: curl http://localhost:8080/rest/hello
0 0
- rest helloworld
- 5分钟构建spring web mvc REST风格HelloWorld
- 5分钟构建spring web mvc REST风格HelloWorld
- Spring Boot——构建REST风格HelloWorld json数据
- rest
- rest
- REST
- REST
- REST
- rest
- REST
- REST
- rest
- rest
- REST
- REST
- REST
- REST
- [TIJ]continue & break
- android binder机制之--(我是Service Manager)
- ruby之三元运算符写法
- 1:距离排序
- java文件读写操作大全
- rest helloworld
- DataTable转出到Excel
- android进阶(一)Activity加载模式
- virtual box 添加新网卡后网卡名称由eth0改为eth2
- 公钥和私钥的解释
- 【推荐】使用bitnami来搭建自己的server
- 堆排序,大顶堆,小顶堆
- OLE剪贴板学习心得
- 内部文件