如何实现支持REST的Java Business Services
business services 根据类型的不同有不同的内容,主要的内容有:
专业性服务。包括:咨询服务,法律服务,会计、审计和摘记服务,税收服务,维修服务,与工程有关的专业服务等等。
计算机相关服务。包括:计算机硬件安装的咨询服务,数据处理服务,软件实施服务。
研究与开发服务
不动产服务。以房地产服务为主。
设备租赁服务。
其他服务。包括:广告服务,摄影服务,包装服务,会议服务,翻译服务,与农、林、牧、渔的附属服务、相关科学技术咨询服务,管理咨询服务,技术检测及分析服务,近海石油服务、地质、地球物理和其他科学勘探服务,陆上石油服务等等。
REST是http一个接口的规范,你只要对你JAVA实现的http接口,遵循REST规范就可以了
具体规范大致是get请求表示查询数据 ,POST请求表示对数据赋值,也就是新增或者更新数据,DELETE 请求就是删除数据。
其中java实现http接口,有很多种方式,你可以用传统的servlet,或者spring框架来做,都可以
下图显示了示例实现中的类。蓝色所示的类是框架外部的类,将它们放在这里是为了展示与框架的结构关系。
配置文件
配置文件 "rest-services-config.xml" 包含 REST 服务表示形式和相应的Java Action之间的映射,如下:
清单 1. REST 服务配置
以下是引用片段:
<?xml version="1.0" ?>
<rest-config>
<rest-api id="CreateUserProfile" uri="/Registration/CreateUser" method="POST">
<handler id="RegAction" class="ws.registration.restactions.CreateProfile"/>
</rest-api>
<rest-api id="GetUserProfile" uri="/Registration/GetUser" method="GET">
<handler id="RegAction" class=" ws.registration.restactions.GetProfile"/>
</rest-api>
...
</rest-config>
在该示例实现中,XML Binding服务实现在"rl-config.xml"文件中配置的框架配置文件如下所示。通过修改此文件实现的任何自定义实现都可以接入,只要实现了XMLBindingService接口。
清单 2:框架配置
以下是引用片段:
# XML Binding Implementation Service
# Default implementation
ws.rest.xmlbinding.service.impl=ws.rest.xmlbinding.service.impl.XMLEncDecServiceImpl
日志配置文件 "ws_log.properties" 指定log4j属性和日志文件的位置。这可以按需要作出适当修改。
Controller Servlet
RESTServiceServlet在web.xml中配置,处理所有具有上下文路径的请求,其中上下文路径的<web-app>/restservices/*如下所示:
清单 3:Servlet配置
以下是引用片段:
<servlet>
<description></description>
<display-name>RESTServletService</display-name>
<servlet-name>RESTServletService</servlet-name>
<servlet-class>ws.rest.servlet.RESTServiceServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RESTServletService</servlet-name>
<url-pattern>/restservices/*</url-pattern>
</servlet-mapping>
REST Action
对于每个REST资源,例如 GetUserProfile,都将创建一个实现ActionInterface的相应动作类。该接口定义了动作类需要实现的 "doExecute(ActionContext ctx)" 方法。ActionContext提供服务,获取 REST 路径输入或查询参数,获取XMLBindingService实例并将XML输出发送到客户端,不公开Action的协议细节。PathInputs是一个包含路径值的List对象,路径值的顺序与它们在URL中指定的顺序相同。
清单 4:Action代码片段
以下是引用片段:
public class GetProfile implements ActionInterface {
public void doExecute(ActionContext context) throws Exception {
// Get the value from URL path
String userName = context.getPathInputs().get(0);
// Invoke backend service to retrieve user profile
UserProfileBean bean = getUser(userName);
// Serialize the bean using framework service and send response
String xml = context.getXMLBindingService().serialize(bean);
// Use the ActionContext to generate XML and
context.sendResponse(response, xml);
}
动作类负责使用超类中的XMLBindingService以XML形式生成输出。请查看示例实现的ws.registration.restactions.GetProfile类。ActionContext还可以提供协议特定的HttpServletRequest和HttpServletResponse对象,以防需要自定义处理。它还提供了Path值和URL参数。
XML Binding
该代码示例提供了一个Java XML Binding的实现,该实现使用java.beans.XMLEncoder和java.beans.XMLDecoder类。XML Binding服务实现接受一个JavaBean对象,并将其转换为上述Encoder和Decoder相应的XML表示形式。如果需要JAXB实现,那么可以开发一个实现 ws.rest.xmlbinding.service.XMLBindingService接口的实现类。
执行示例服务
示例代码分发包含示例WAR文件"RESTWS.war",它可以部署在Tomcat容器中(已在Apache Tomcat版本6.0.20上进行了测试)。JDK要求是JDK 1.5以上。
成功部署该应用程序之后,在浏览器中输入URL:
图 5. 创建Profile Service输入
该页面调用REST服务
POST <url-prefix>/Registration/CreateProfile
您可以修改在 <string></string> 标记中指定的XML值。
注意:请注意XML结构依赖于JavaBean对象和Java使用的XML序列化技术。
提交时,动作类显示成功消息,表示后端服务的调用。可以查看 ws_log.log 文件调试消息。
图 6. 创建Profile Service输出
类似地,实现示例GET <url-prefix>/Registration/GetProfile/{username}服务以检索配置文件,如下图所示:
转载仅供参考,版权属于原作者。祝你愉快,满意请采纳哦
实现支持REST的Java Business Services方法有多种,推荐使用Spring 3框架实现。其他几种实现技术来创建 Java 的 RESTful Web Services,如 RestEasy 和 Jersey。Jersey 是其中最值得注意的,它是 JAX-RS(JSR 311)的参考实现,也可使用 Restlet 框架和从头开始开发。
Spring REST 支持的主要特性包括:
- 注释,如 @RequestMapping 和 @PathVariable,支持资源标识和 URL 映射
ContentNegotiatingViewResolver 支持为不同的 MIME/内容类型使用不同的表示方式
使用相似的编程模型无缝地整合到原始的 MVC 层。
参考实现如下:
@Controller
public class EmployeeController {
@RequestMapping(method=RequestMethod.GET, value="/employee/{id}")
public ModelAndView getEmployee(@PathVariable String id) {
Employee e = employeeDS.get(Long.parseLong(id));
returnnew ModelAndView(XML_VIEW_NAME, "object", e);
}
@RequestMapping(method=RequestMethod.POST, value="/employee")
public ModelAndView addEmployee(@RequestBody String body) {
Source source = new StreamSource(new StringReader(body));
Employee e = (Employee) jaxb2Mashaller.unmarshal(source);
employeeDS.add(e);
returnnew ModelAndView(XML_VIEW_NAME, "object", e);
}
@RequestMapping(method=RequestMethod.PUT, value="/employee/{id}")
public ModelAndView updateEmployee(@RequestBody String body) {
Source source = new StreamSource(new StringReader(body));
Employee e = (Employee) jaxb2Mashaller.unmarshal(source);
employeeDS.update(e);
returnnew ModelAndView(XML_VIEW_NAME, "object", e);
}
@RequestMapping(method=RequestMethod.DELETE, value="/employee/{id}")
public ModelAndView removeEmployee(@PathVariable String id) {
employeeDS.remove(Long.parseLong(id));
List<Employee> employees = employeeDS.getAll();
EmployeeList list = new EmployeeList(employees);
returnnew ModelAndView(XML_VIEW_NAME, "employees", list);
}
@RequestMapping(method=RequestMethod.GET, value="/employees")
public ModelAndView getEmployees() {
List<Employee> employees = employeeDS.getAll();
EmployeeList list = new EmployeeList(employees);
returnnew ModelAndView(XML_VIEW_NAME, "employees", list);
}
web.xml
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
rest-servlet-spring-bean.xml
<context:component-scan base-package="dw.spring3.rest.controller" />
<!--To enable @RequestMapping process on type level and method level-->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!--Use JAXB OXM marshaller to marshall/unmarshall following class-->
<bean id="jaxbMarshaller"
class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>dw.spring3.rest.bean.Employee</value>
<value>dw.spring3.rest.bean.EmployeeList</value>
</list>
</property>
</bean>
<bean id="employees" class=
"org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg ref="jaxbMarshaller" />
</bean>
<bean id="viewResolver" class=
"org.springframework.web.servlet.view.BeanNameViewResolver" />
REpresentational State Transfer (REST)是一种架构原则,其中将web服务视为资源,可以由其URL唯一标识。RESTful Web服务的关键特点是明确使用 HTTP 方法来表示不同的操作的调用。
REST 的基本设计原则对典型 CRUD 操作使用 HTTP 协议方法:
POST - 创建资源insert
GET - 检索资源select
PUT – 更新资源update
DELETE - 删除资源delete
REST 服务的主要优势在于:
它们是跨平台 (Java、.net、PHP 等)高度可重用的,因为它们都依赖基本 HTTP 协议。
它们使用基本的 XML,而不是复杂的 SOAP XML,使用非常方便。