webservices - validating input

2019-07-25 11:53发布

问题:

I am playing around with a simple webservice JAX-WS without using any fancy tools ( no usage of CXF or Metro etc )

I am using inbuilt support provided by JDK 1.6.x to create this web service ( JAX-WS RI 2.1.6 in JDK 6 )

My question - I would like to introduce validations in my web service so I thought I would use the "@SchemaValidation"

Tried importing it as well - but code fails to compile

Snapshot of my code:

package com.mkyong.ws;
import javax.jws.WebService;
**import com.sun.xml.ws.developer.SchemaValidation;**


@WebService(endpointInterface = "com.mkyong.ws.HelloWorld")
**@SchemaValidation()**  
public class HelloWorldImpl implements HelloWorld{

While compiling I get the exception :

javac com\mkyong\ws\HelloWorldImpl.java
com\mkyong\ws\HelloWorldImpl.java:3: package com.sun.xml.ws.developer does not exist
import com.sun.xml.ws.developer.SchemaValidation;
                           ^
com\mkyong\ws\HelloWorldImpl.java:8: cannot find symbol
symbol: class SchemaValidation
@SchemaValidation()
^
2 errors

So my question is : Can we not use this annotation with JDK1.6 implementation of RI ? Do I need to import any special jar's in my classpath ? Thanks, satish

回答1:

According to JarFinder:

http://www.jarfinder.com/index.php/java/info/com.sun.xml.ws.developer.SchemaValidation

  • jaxws-rt-2.1.4.jar
  • jaxws-rt-2.1.3.jar

Edit - I do not know what exactly that annotation is used for but:

  1. It is possible to use JAXB (hence JDK 1.6 alone) to validate an XML payload using the Unmarshaller class. Have a look to Unmarshaller.setSchema(), I also have some examples, let me know if you need them.
  2. Judging from the annotation's namespace, it looks more like something internal (com.sun.xml...)


回答2:

Try to consider to use a wsdl. In the wsdl You can add all kind of validations. The validation rules in the wsdl are independant of the language you use to implement your webservice



回答3:

Use @SchemaValidation from the com.sun.xml.internal.ws.developer.SchemaValidation package on the service implementation bean if you wish to use the annotation shipped with the JDK. I must note though, I've seen at least two instances where people have had issues using the annotation in this package.

The annotation you're attempting to use is bundled with glassfish's JAX-WS RI in the com.sun.xml.ws.developer.SchemaValidation package, so you'll need to include it in your project's classpath if you want to make use of that instead