使用Jersey创建简单的Java RESTful Web服务

In this tutorial you will learn how to create simple Java RESTful web services using Jersey framework.

在本教程中,您将学习如何使用Jersey框架创建简单的Java RESTful Web服务。

JAX-RS is the Java API used for creating RESTful web services. There are mainly two types of implementation of JAX-RS that are Jersey and RESTeasy. In this tutorial we will see Jersey implementation.

JAX-RS是用于创建RESTful Web服务的Java API。 JAX-RS的实现主要有两种类型,分别是Jersey和RESTeasy。 在本教程中,我们将看到Jersey实现。

We will use eclipse to create our web service. Make sure it contains apache tomcat server. You can follow below link to know how to download and configure tomcat.

我们将使用eclipse创建我们的Web服务。 确保它包含Apache Tomcat服务器。 您可以点击以下链接了解如何下载和配置tomcat。

Read: Configure Apache Tomcat Server in Eclipse IDE

阅读: 在Eclipse IDE中配置Apache Tomcat服务器

Create Simple Java RESTful Web Services Using Jersey 1

Also Read: Create Java SOAP Web Service Using Eclipse

另请阅读: 使用Eclipse创建Java SOAP Web服务

如何使用Jersey框架创建Java RESTful Web服务 (How to Create Java RESTful Web Services Using Jersey Framework)

1. First of all download the Jersey jars from below link.

1.首先从下面的链接下载Jersey jars。

https://jersey.java.net/download.html

https://jersey.java.net/download.html

2. Open eclipse and go to File -> New -> Dynamic Web Project to create a dynamic web project with name JavaRESTfullWS.

2.打开eclipse并转到File-> New- > Dynamic Web Project以创建一个名称为JavaRESTfullWS的动态Web项目。

3. Now we have to import jersey jars in our project. The file that you have downloaded in previous step is compressed file. After extracting it you will get several folders. Just import all the jars present in those folders. You can follow below link if you don’t know how to import jars in eclipse.

3.现在我们必须在我们的项目中导入球衣瓶。 您在上一步中下载的文件是压缩文件。 解压缩后,您将获得几个文件夹。 只需导入这些文件夹中存在的所有jar。 如果您不知道如何在eclipse中导入jar,可以单击以下链接。

http://stackoverflow.com/questions/4440479/how-can-i-add-jar-files-to-the-web-inf-lib-folder-in-eclipse

http://stackoverflow.com/questions/4440479/how-can-i-add-jar-files-to-the-web-inf-lib-folder-in-eclipse

4. Create a package with name example under src folder in your project. Under this package create a source file with name DemoService and add following code inside it.

4.在项目的src文件夹下创建一个名称示例包。 在此程序包下,创建一个名称为DemoService的源文件,并在其中添加以下代码。

DemoService.java

DemoService.java

package example;
 
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
 
@Path("/DemoService")
public class DemoService {
		
	@GET
	@Path("/{msg}")
	@Produces(MediaType.TEXT_HTML)
	public String printMsg(@PathParam("msg") String msg){
		return "<h1>Your message: "+msg+"</h1>";
	}	
}

5. Enter following code in web.xml file. You can find it under Web-Content -> WEB-INF folder. If it is not there then create one.

5.在web.xml文件中输入以下代码。 您可以在Web-Content-> WEB-INF文件夹下找到它。 如果不存在,则创建一个。

Web.xml

Web.xml

<?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"   
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">  
 
 <servlet>  
    <servlet-name>Jersey REST Service</servlet-name>  
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>  
    <init-param>  
        <param-name>jersey.config.server.provider.packages</param-name>  
        <param-value>example</param-value>  
    </init-param>  
    <load-on-startup>1</load-on-startup>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>Jersey REST Service</servlet-name>  
    <url-pattern>/rest/*</url-pattern>  
  </servlet-mapping>  
</web-app>

The project has following structure.

该项目具有以下结构。

Create Simple Java RESTful Web Services Using Jersey 2

6. Right click on your project folder and then select Run As -> Run on Server.

6.右键单击项目文件夹,然后选择运行方式->在服务器上运行

7. For testing the web service open following url in browser.

7.要测试Web服务,请在浏览器中打开以下URL。

http://localhost:8080/JavaRESTfullWS/rest/DemoService/Hello

http:// localhost:8080 / JavaRESTfullWS / rest / DemoService / Hello

Create Simple Java RESTful Web Services Using Jersey 3

You can change Hello at the end of the url to pass some other message as parameter.

您可以在网址末尾更改Hello,以传递其他消息作为参数。

Video Tutorial

影片教学

You can watch below video. Everything is explained in it step by step.

您可以观看下面的视频。 一切都在其中逐步说明。

Comment below if you are facing any difficulty to create Java RESTful web services.

如果在创建Java RESTful Web服务时遇到任何困难,请在下面评论。

Happy Coding!! 🙂 🙂

快乐编码! 🙂

翻译自: https://www.thecrazyprogrammer.com/2016/06/create-simple-java-restful-web-services-using-jersey.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值