Search code examples
javaweb-servicesweblogicjax-ws

JAX-WS Validate Schema in WebLogic with @SchemaValidation


I wasn't able to find the solution and I found more people stuck in the same problem so I will post it here.


By default a JAX-WS server (at least for WebLogic) will not validate the message received with its associated schema.

This can lead to a lot of problems since any invalid value (wrong xsd:dateTime format, letters on a number field, etc) will result in a null value in the Java object, including mandatory fields.

What I need to do is a simple validation that should be provided by the server.

import com.sun.xml.internal.ws.developer.SchemaValidation;

@Stateless
@WebService(portName="ValidatedService")
@SchemaValidation
public class ValidatedService {

    public void operation(@WebParam(name="request") ValidatedRequest request) {

        /* do stuff */
    }
}

For some reason when I was trying to use the provided schema validation I was getting the following exception:

Caused By: javax.xml.ws.WebServiceException: Annotation @com.sun.xml.internal.ws.developer.SchemaValidation(handler=class com.sun.xml.internal.ws.server.DraconianValidationErrorHandler) is not recognizable, atleast one constructor of class com.sun.xml.internal.ws.developer.SchemaValidationFeature should be marked with @FeatureConstructor

I do not wish to implement any custom validator. The server should provided this type of service with simple and straightforward configuration.


Solution

  • The problem was: I was using the wrong package for @SchemaValidation.

    The correct class that worked for me is com.sun.xml.ws.developer.SchemaValidation, which is provided in the file mw_home\modules\glassfish.jaxws.rt_1.3.0.0_2-1-5.jar (using WLS 10.3.6).

    In the previous code segment I was referencing the wrong package: com.sun.xml.internal... but using the one provided by WebLogic worked instantly.

    If you are using Maven and using the bundled JAR as dependency you might not have this library in the classpath, which led me to the problem. You need to add it to your classpath via dependency (provided scope only) and reference the correct package for that class name in your JAX-WS WebService class (an abstract class won't do it).

    More information in the Enabling Schema Validation on the Server page.

    This schema validation is enough for me at the moment since I do not need any custom behavior.