这篇文章将提供struts2和rest web service集成事例,struts2提供Rest插件去集成Rest web Service。父包应该设置为rest-default.惯例action类名称填写Controller前缀。struts2 rest web service支持不同类型的相应结果类型。
运行应用需要的软件
为了运行实例,我们需要如下软件:
1、java 7
2、tomcat 7
3、eclipse
4、maven
eclipse项目结构
找到struts2和REST web service在eclipse中集成的结构。
Struts2 Rest插件:pom.xml
为了解释struts2 REST API,我们需要在pom.xml文件中添加如下插件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.concretepage</groupId>
<artifactId>struts2rest</artifactId>
<packaging>war</packaging>
<version>1</version>
<name>Struts2Rest</name>
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.3.20</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-rest-plugin</artifactId>
<version>2.3.20</version>
</dependency>
</dependencies>
</project>
为了支持struts2 rest插件,我们需要添加对struts2约定插件的解释依赖插件。
struts2.xml :配置约定插件
在项目中找到struts2.xml,配置struts约定插件配置项。
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.convention.action.suffix" value="Controller"/>
<constant name="struts.convention.action.mapAllMatches" value="true"/>
<constant name="struts.convention.default.parent.package" value="rest-default"/>
<constant name="struts.convention.package.locators" value="rest"/>
</struts>
找到约定插件的描述,并且开始使用。
<constant name="struts.convention.action.suffix" value="Controller"/>
定义一个action类前缀。rest web service的action类约定使用Controller前缀并且这个前缀将不包括在URL内。
<constant name="struts.convention.action.mapAllMatches" value="true"/>
映射匹配,可以设置值true或false
<constant name="struts.convention.default.parent.package" value="rest-default"/>
为了支持REST Web service,struts2 继承rest-default父包名;
<constant name="struts.convention.package.locators" value="rest"/>
locator表示哪一个controller类的包名中最后一个单词存在设置的值。在我们的class中,它被设置为rest
创建一个Employee实体库
对于实例应用,我们创建一个Employee实体库。先找到employee类.
Employee.java
package com.concretepage.rest;
public class Employee {
private Integer id;
private String name;
private String company;
public Employee(Integer id, String name, String company){
this.id =id;
this.name = name;
this.company = company;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
}
现在找到用简单数据构成的数据层。
EmployeeRepository.java
package com.concretepage.rest;
import java.util.HashMap;
import java.util.Map;
public class EmployeeRepository {
private static Map<String,Employee> map = new HashMap<String,Employee>();
public EmployeeRepository(){
map.put("111", new Employee(111, "Ram", "ABC"));
map.put("222", new Employee(222, "Shyam", "EFG"));
map.put("333", new Employee(333, "Mohan", "XYZ"));
}
public Employee getEmployeeById(String id){
return map.get(id);
}
public Map<String,Employee> findAllEmployee(){
return map;
}
}
注意:文章到这里还没有完,由于篇幅限制,完整内容请到hongfu951博客上查看
完整内容URL地址:Struts2和REST Web Service集成事例