欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【叨、校长】创建基于RestEasy框架的Rest应用

程序员文章站 2022-07-16 22:34:46
...

本教程基于RestEasy官方示例!通俗易懂、老少皆宜、不论男女!---叨、校长

1、用MyEclipse创建一个Java Web项目!

2、导入RestEasy框架所需要的JAR包(点击下载)

3、配置web.xml文件

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<!-- 手动注册Rest接口 -->
	<context-param>
		<param-name>resteasy.resources</param-name>
		<param-value>com.daodao.rest.HelloWorld</param-value>
	</context-param>
	<context-param>
		<param-name>resteasy.servlet.mapping.prefix</param-name>
		<param-value>/rest</param-value>
	</context-param>

	<listener>
		<listener-class>
			org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
	</listener>

	<servlet>
		<servlet-name>resteasy-servlet</servlet-name>
		<servlet-class>
			org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>resteasy-servlet</servlet-name>
		<url-pattern>/rest/*</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 4、编写测试接口:

 

package com.daodao.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/HelloWorld")
public class HelloWorld {
	
	@GET
	@Path("/sayHello")
	public String sayHello(){
		return "Hello World";
	}
}

 5、部署项目,测试应用:

http://localhost:8080/MyRest/rest/HelloWorld/sayHello