使用WebSphere Application Server构建简单的,兼容JAX-RS 2.0的REST服务

RESTful Web服务的Java™API(JAX-RS)是接口和Java注释的集合,可简化服务器端REST应用程序的开发。 感谢JAX-RS,您可以通过注释类来响应特定的HTTP方法(例如GET或POST)和统一资源标识符(URI)模式来指定RESTful Web服务行为。 JAX-RS的当前版本是2.0版,并且是Java Platform Enterprise Edition(Java EE)7中必需的API。

本文演示了如何使用IBM®WebSphere®Application Server内置的功能(例如对象反序列化,定制序列化和异常处理)创建简单的,兼容JAX-RS 2.0的REST服务。 本文还包括三个Eclipse项目,可以从GitHub下载这些项目,在阅读本文时可以遵循这些项目。

可下载的项目文件

在阅读本文之前,请从GitHub下载三个项目文件。 这三个项目是EAR项目,EJB项目和Web项目。 通过下载这些项目,您可以在阅读本文时按照它们进行操作。 这些项目文件是从Eclipse 4.5.2(火星)IDE与传统的Mars WebSphere Application Server V9.x开发人员工具(可从Eclipse Marketplace获得)导出的。

EJB项目中的persistence.xml文件配置为使用WebSphere Application Server V9提供的内置Derby JTA数据源。 如果要配置和使用其他JTA数据源,请在WebSphere Application Server V9管理控制台中配置JTA数据源,然后编辑persistence.xml文件以指定新的JTA数据源。

WebSphere Application Server中的Java EE 7 JAX-RS功能

从WebSphere Liberty Profile v8.5.5.6和传统的WebSphere Application Server V9开始,提供了对JAX-RS 2.0的支持。 这些WebSphere Application Server运行时提供以下功能:

  • 基于Apache CXF 3.0的JAX-RS 2.0服务器运行时
  • 内置的普通旧Java对象(PO​​JO)实体提供程序支持,由Jackson JSON处理器提供
  • 内置的IBM JSON4J实体提供程序支持

JAX-RS 2.0引入了以下新功能:

  • 客户端API
  • 异步请求处理
  • 超媒体支持(超媒体作为状态引擎(HATEOS))
  • 新注释(增强了对上下文和依赖注入(CDI)的支持)
  • Bean验证
  • 过滤器和处理程序
  • 更丰富的内容协商

设计和实现一个简单的RESTful应用程序

在本文中,您将学习如何设计和实现学生信息系统的基础,该系统是学生注册应用程序(SRA)。 SRA演示了JAX-RS 2.0提供的一些新功能,特别是HATEOAS支持以及改进的注入和bean验证。

下图说明了应用程序的组件及其高层交互。

图1.学生注册申请组件
学生注册申请组件

SRA提供以下功能来管理学生注册:

  • 新生注册
  • 编辑注册的学生信息
  • 列出注册学生
  • 删除学生注册

SRA由以下组件组成:

  • 提供RESTful服务的Web模块
  • 提供数据库创建,检索,更新和删除逻辑的EJB模块
  • 保存学生记录的数据库

JAX-RS运行时使用StudentRegistrationApplication配置类来确定SRA中的类,即资源类和提供者类。 以下清单显示了其中注册了资源和提供者类的StudentRegistrationApplication类。

清单1. StudentRegistrationApplication配置类
@ApplicationPath("rest")
public class StudentRegistrationApplication extends Application {
}

WebSphere Application Server V9提供了JAX-RS 2.0感知的Web容器,因此StudentRegistrationApplication类上的@ApplicationPath批注将用于将/rest/* URL模式映射到StudentRegistrationApplication类。 然后,WebSphere Application Server Web容器会自动确保对/rest/* URL模式的引用会启动适当的资源类。

不需要其他配置,WebSphere Application Server Web容器在启动过程中扫描资源类( @Path @Provider )和提供程序类( @Provider )。

SRA具有单个资源类,即StudentResource类。 StudentResource类代表注册学生的整个集合。 以下清单显示了该类的初始实现。

清单2.学生资源类
@Path( "/students" )
@Stateless
public class StudentResource {

    private static final String LI
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Table of Contents IBM WebSphere Application Server - The Complete Reference Part I - Getting Started Chapter 1 - What Is WebSphere? Chapter 2 - Installing and Starting WebSphere Chapter 3 - Hello World (Wide Web) Chapter 4 - Hello Servlets and JSPs Chapter 5 - Hello EJBs Part II - WebSphere Technologies Chapter 6 - Web Servers and Java Application Servers Chapter 7 - Servlets and JavaServer Pages Chapter 8 - Common Object Request Broker Architecture Chapter 9 - Internet Inter-ORB Protocol Chapter 10 - JavaBeans Chapter 11 - Enterprise JavaBeans Chapter 12 - Extensible Markup Language (XML) Chapter 13 - Java Security Chapter 14 - Java Messaging Service Chapter 15 - Java 2 Enterprise Edition Part III - Development Environments for WebSphere Applications Chapter 16 - WebSphere Studio Application Developer Chapter 17 - VisualAge for Java Part IV - Core Services Chapter 18 - Connection Pooling and Data Access Beans Chapter 19 - WebSphere Security Services Chapter 20 - Lightweight Directory Access Protocol Support Chapter 21 - WebSphere Application Logging Part V - Developing Servlets and JavaServer Pages Chapter 22 - HTTP Servlets Chapter 23 - Servlet Sessions Chapter 24 - WebSphere-Specific Servlet Issues Chapter 25 - JSP Elements Chapter 26 - JSP Examples Chapter 27 - Custom Tag Libraries Chapter 28 - Debugging Servlets and JSPs Using VisualAge Part VI - Developing EJB Chapter 29 - Session Beans Chapter 30 - Entity Beans Part VII - Using XML Chapter 35 - Writing XML E-Business Applications Chapter 36 - Application of XSL Transformations Chapter 37 - Web Services: SOAP, WSDL, and UDDI Chapter 38 - Deploying Web Services on WebSphere Part VIII - Internationalization and Localization Chapter 39 - Java Support Features for Internationalization Chapter 40 - Building Multilingual Applications Part IX - Administering WebSphere Sites Chapter 41 - Deploying WebSphere for Scalability and Fault Tolerance Chapter 42 - The WebSphere Administrative Console Chapter 43 - WebSphere Administration Scripts Chapter 44 - Administering WebSphere Security
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值