Using ID and IDREF in your WSDLs response messages with JAXB
For one of my projects, I have implemented the ID/IDREF XML pattern inside one of my Web Services. When I created with JDeveloper a Web Service from it everything looked fine, no errors or what soever. I could receive this pattern inside my request messages without any problems. After I filled in the same String inside the IDREF as I did in ID the only response I get is a HTTP 200 without any body. For getting more information about the problem I used a custom SchemaValidationHandler inside my Web Service implementation.
@SchemaValidation( handler = SchemaValidationErrorHandler.class )
The source code for the custom SchemaValidationErrorHandler is:
package nl.whitehorses.idref.handler;
import com.sun.xml.ws.developer.ValidationErrorHandler;
import org.xml.sax.SAXParseException;
public class SchemaValidationErrorHandler extends ValidationErrorHandler {
public static final String WARNING = "SchemaValidationWarning";
public static final String ERROR = "SchemaValidationError";
public static final String FATAL_ERROR = "SchemaValidationFatalError";
public void warning(SAXParseException exception) {
packet.invocationProperties.put(WARNING, exception);
}
public void error(SAXParseException exception) {
packet.invocationProperties.put(ERROR, exception);
}
public void fatalError(SAXParseException exception) {
packet.invocationProperties.put(FATAL_ERROR, exception);
}
}
After using the schema validation I got response from the Web Service it was a SAXparser Exception. After some puzzling I figured out that it was the relationship between the ID and the IDREF. The IDREF couldn’t find the the ID during marshalling of the response message. I was looking around the internet for two days but I didn’t find any direct answer to my problem. What I found strange is that the ID is always a String and the IDREF is a Object. And in the examples the IDREF was a specified Object like a Car a Book or a Employee. After seeing this multiple times I put in the parent object of the ID inside the IDREF.
Here is a example of the code where I implemented the ID/IDREF pattern.
public GetAllEmployeesResponseMessageType operation(GetAllEmployeesRequestMessageType input) throws FaultMessage {
GetAllEmployeesResponseMessageType employeeResponse = new GetAllEmployeesResponseMessageType();
employeeResponse.setCompanies(setCompanies());
for(CompanyType company : employeeResponse.getCompanies().getCompany())
{
employeeResponse.setEmployees(setEmployees(company));
}
return employeeResponse;
}
private GetAllEmployeesResponseMessageType.Companies setCompanies() {
GetAllEmployeesResponseMessageType.Companies companies = new GetAllEmployeesResponseMessageType.Companies();
companies.getCompany().add(setCompany());
return companies;
}
private GetAllEmployeesResponseMessageType.Employees setEmployees(CompanyType company) throws FaultMessage {
GetAllEmployeesResponseMessageType.Employees employees = new GetAllEmployeesResponseMessageType.Employees();
employees.getEmployee().add(setEmployee(company));
return employees;
}
private CompanyType setCompany() {
CompanyType company = new CompanyType();
company.setAddress(setAddress());
company.setDepartment(setDepartmentComp());
company.setId("hello123");
company.setName("name");
company.setPhoneNumber("0123456789");
return company;
}
private EmployeeType setEmployee(CompanyType company) throws FaultMessage {
EmployeeType employee = new EmployeeType();
employee.setCommissionPct(new BigDecimal("0.25"));
employee.setDepartment(setDepartment());
employee.setEmail("email@adres.com");
employee.setFirstName("first name");
employee.setHireDate(getXMLGregorianCalendarNow());
employee.setJob(setJob());
employee.setLastName("last name");
employee.setPhoneNumber("0123456789");
employee.setSalary(new BigDecimal("1234.25"));
employee.setCompanyId(company);
return employee;
}
After that I got response from the Web Service with the value of the ID inside the IDREF attribute.
<ns0:GetAllEmployeesResponseMessage xmlns:ns2="nl.whitehorses.entity.company" xmlns:ns1="nl.whitehorses.entity.employee" xmlns:ns0="nl.whitehorses.message.employee">
<Companies>
<Company id="hello123">
<Name>name</Name>
<Address>
<street>street</street>
</Address>
<PhoneNumber>0123456789</PhoneNumber>
<Department>
<id>1</id>
<description>company department</description>
</Department>
</Company>
</Companies>
<Employees>
<Employee CompanyId="hello123">
<FirstName>first name</FirstName>
<LastName>last name</LastName>
<Email>email@adres.com</Email>
<PhoneNumber>0123456789</PhoneNumber>
<HireDate>2014-08-08T12:28:50.533+02:00</HireDate>
<Salary>1234.25</Salary>
<CommissionPct>0.25</CommissionPct>
<Job>
<id>AC_ACCOUNT</id>
<description>dit is een job</description>
</Job>
<Department>
<id>2</id>
<description>employee department</description>
</Department>
</Employee>
</Employees>
</ns0:GetAllEmployeesResponseMessage>
Overzicht blogs
Geen reacties
Geef jouw mening
Reactie plaatsenReactie toevoegen