spring integration之http-rest例子解析(三)

2014-11-24 10:09:05 · 作者: · 浏览: 3
channel="employeeSearchRequest"
output-channel="employeeSearchResponse"
ref="employeeSearchService"
method="getEmployee"
requires-reply="true"
send-timeout="60000"/>


代码解析
把配置看明白了,代码就很简单了,看下JAVA代码,
Employee.java
[java]
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"employeeId",
"fname",
"lname"
})
@XmlRootElement(name = "Customer")
public class Employee {

private Integer employeeId;
private String fname;
private String lname;

public Employee() {}

public Employee(Integer employeeId, String fname, String lname) {
this.employeeId = employeeId;
this.fname = fname;
this.lname = lname;
}
//get and set ...
}

EmployeeList
[java]
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"employee",
"returnStatus",
"returnStatusMsg"
})
@XmlRootElement(name = "EmployeeList")
public class EmployeeList {

@XmlElement(name = "Employee", required = true)
private List employee;

@XmlElement(name = "returnStatus", required = true)
private String returnStatus;

@XmlElement(name = "returnStatusMsg", required = true)
private String returnStatusMsg;

/**
* @return the employee
*/
public List getEmployee() {
if (employee == null){
employee = new ArrayList();
}
return employee;
}
}

EmployeeSearchService
[java]
@Service("employeeSearchService")
public class EmployeeSearchService {

private static Logger logger = Logger.getLogger(EmployeeSearchService.class);
/**
* The API getEmployee() looks up the mapped in coming message header's id param
* and fills the return object with the appropriate employee details. The return
* object is wrapped in Spring Integration Message with response headers filled in.
* This example shows the usage of URL path variables and how the service act on
* those variables.
* @param inMessage
* @return an instance of {@link Message} that wraps {@link EmployeeList}
*/
@Secured("ROLE_REST_HTTP_USER")
public Message getEmployee(Message< > inMessage){

EmployeeList employeeList = new EmployeeList();
Map responseHeaderMap = new HashMap();

try{
MessageHeaders headers = inMessage.getHeaders();
String id = (String)headers.get("employeeId");
boolean isFound;
if (id.equals("1")){
employeeList.getEmployee().add(new Employee(1, "John", "Doe"));
isFound = true;
}else if (id.equals("2")){
employeeList.getEmployee().add(new Employee(2, "Jane", "Doe"));
isFound = true;
}else if (id.equals("0")){
employeeList.getEmployee().add(new Employee(1, "John", "Doe"));
employeeList.getEmployee().add(new Employee(2, "Jane", "Doe"));
isFound = true;
}else{
isFound = false;
}
if (isFound){
setReturnStatusAndMessage("0", "Success", employeeList, responseHeaderMap);
}e