3
votes

I am creating a SOAP service using JAX-WS and JAXB, by annotating the classes. My service implementation looks like this:

@Stateless
@Local(WSApplicationService.class)
@WebService(name = "WSApplicationService", serviceName = "WSApplicationService")
@BindingType(SOAPBinding.SOAP11HTTP_BINDING)
public class WSApplicationServiceImpl implements WSApplicationService {

  @Override
  @WebMethod
  @WebResult(name = "reference")
  public long submitApplication(@WebParam(name = "application") ApplicationSubmission application) throws SOAPException {
    ...
  }
}

After asking this question, I learned that there's no way to specify certain restrictions on the input data using JAXB annotations. However, I need to restrict certain String properties to patterns (mainly postcodes and phone numbers). Therefore, I'm looking to customize the WSDL generated by above class. I can't find a way to access the WSDL though. Is there a way to override or customize the JAX-WS generated WSDL?

1

1 Answers

5
votes

While you can safely have the WSDL generated automatically, it usually makes sense to hand-code the XSD referenced from the WSDL, which gives you all the richness of expression that a schema generated from JAXB classes can't, and also ensures the schema doesn't change when you don't want it to. (This practice is called contract-first, whereas starting with the classes is called code-first.)

When you do contract-first, you can generate your JAXB classes from the XSD automatically as part of your build process, making sure they're always in sync. You can customize the generated classes using XJB files.

In case you're using Maven, generating the classes could look like this:

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.7.5</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <schemaDirectory>src/main/resources/wsdl</schemaDirectory>
        <schemaIncludes>
            <include>*.xsd</include>
        </schemaIncludes>
        <verbose>true</verbose>
        <extension>true</extension>
    </configuration>
</plugin>

Of course you can also use your hand-coded classes with a hand-coded schema definition (by telling the marshaller where to find it). I don't think you can take influence on the WSDL generation by other means.