我正在创建一个Web服务,其中调用我的服务的客户端需要SOAP响应中每个元素的名称空间前缀。
在简单的示例中,客户端需要这样的响应:
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<dlwmin:GetEmployeesByDeptResponse xmlns:dlwmin="http://tempuri.org/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<dlwmin:EmployeesListResult>
<dlwmin:Employee>
<dlwmin:firstName>John</dlwmin:firstName>
<dlwmin:lastName>Doe</dlwmin:lastName>
</dlwmin:Employee>
<dlwmin:Employee>
<dlwmin:firstName>Audrey</dlwmin:firstName>
<dlwmin:lastName>Gibson</dlwmin:lastName>
</dlwmin:Employee>
</dlwmin:EmployeesListResult>
</dlwmin:GetEmployeesByDeptResponse>
</soapenv:Body>
</soapenv:Envelope>
但我目前的回应是:
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<dlwmin:GetEmployeesByDeptResponse xmlns:dlwmin="http://tempuri.org/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<EmployeesListResult>
<Employee>
<firstName>John</firstName>
<lastName>Doe</lastName>
</Employee>
<Employee>
<firstName>Audrey</firstName>
<lastName>Gibson</lastName>
</Employee>
</EmployeesListResult>
</dlwmin:GetEmployeesByDeptResponse>
</soapenv:Body>
</soapenv:Envelope>
我的SOAP服务(JAX-WS)部署在WebSphere Application Server 9.0网络部署(FixPack 5)上,源代码如下所示:
项目结构
employees search.Java
package com.pp.endpoints;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.xml.ws.BindingType;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
import com.pp.entities.Employee;
import com.pp.entities.Employees;
@WebService(targetNamespace = "http://tempuri.org/", name = "EmployeesSearch",
portName = "EmployeesSearchPort", serviceName = "EmployeesSearchService",
wsdlLocation = "WEB-INF/wsdl/EmployeesSearchService.wsdl")
@SOAPBinding(style = Style.DOCUMENT)
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
public class EmployeesSearch {
@WebMethod(operationName = "GetEmployeesByDeparetment", action = "GetEmployeesByDeparetment")
@WebResult(name = "EmployeesListResult")
@RequestWrapper(localName = "GetEmployeesByDeptRequest", className = "GetEmployeesByDeptRequest")
@ResponseWrapper(localName = "GetEmployeesByDeptResponse", className = "GetEmployeesByDeptResponse")
public Employees getEmployeesByDeparetment(@WebParam(name = "department") String department) {
Employees employess = new Employees();
if (department.equals("dev")) {
Employee emp1 = new Employee("John", "Doe");
employess.getEmpList().add(emp1);
Employee emp2 = new Employee("Audrey", "Gibson");
employess.getEmpList().add(emp2);
} else if(department.equals("QA")) {
Employee emp1 = new Employee("Sylvia", "Vinson");
employess.getEmpList().add(emp1);
Employee emp2 = new Employee("Zelenia", "Stark");
employess.getEmpList().add(emp2);
}
return employess;
}
}
get employees by Dept request.Java
package com.pp.endpoints;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "department" })
@XmlRootElement(name = "GetEmployeesByDeptRequest")
public class GetEmployeesByDeptRequest implements Serializable {
private static final long serialVersionUID = -6866935846016764952L;
protected String department;
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}
get employees by Dept response.Java
package com.pp.endpoints;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import com.pp.entities.Employee;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "employeesListResult" })
@XmlRootElement(name = "GetEmployeesByDeptResponse")
public class GetEmployeesByDeptResponse implements Serializable {
private static final long serialVersionUID = -3929452574007113319L;
@XmlElement(name = "EmployeesListResult")
protected Employee employeesListResult;
public Employee getEmployeesListResult() {
return employeesListResult;
}
public void setEmployeesListResult(Employee employeesListResult) {
this.employeesListResult = employeesListResult;
}
}
package-info.Java
@javax.xml.bind.annotation.XmlSchema(namespace = "http://tempuri.org/",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.pp.endpoints;
employee.Java
package com.pp.entities;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Employee", propOrder = { "firstName", "lastName" })
public class Employee implements Serializable {
private static final long serialVersionUID = -1382336004216274895L;
@XmlElement(name = "firstName")
protected String firstName;
@XmlElement(name = "lastName")
protected String lastName;
public Employee(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
employees.Java
package com.pp.entities;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Employees", propOrder = { "empList" })
public class Employees implements Serializable {
private static final long serialVersionUID = -6738577250797101596L;
@XmlElement(name = "Employee", nillable = true)
protected List<Employee> empList = new ArrayList<>();
public Employees() { }
public List<Employee> getEmpList() {
if (empList == null) {
empList = new ArrayList<Employee>();
}
return empList;
}
public void setEmpList(List<Employee> empList) {
this.empList = empList;
}
}
问题:我应该在源代码中更改什么才能获得响应,其中每个元素都有一个名称空间前缀。
请使用HeaderHandler类并添加this this.addToHeader(envelope,header,“dlwmin”,“your endpoint”);
List<Handler> handlerChain = new ArrayList<Handler>();
HeaderHandler hh = new HeaderHandler();
handlerChain.add(hh);
return handlerChain;
例如:qazxsw poi
HeaderHandler