简体   繁体   中英

Validating web service request against XSD in jax-ws?

I am using jax-ws to develop soap based web-services. I have below end point and it has one web method as below.

@WebService
public interface MySoapService {

    @WebMethod
    public List<Result> getResult(TestRequest request);
}

In above web method, TestRequest is generated from my XSD using JAXB . My questions is as this webservice is exposed and which takes JAXB generated object as an input, do i need to perform any validation against XSD ?

If my method takes XML string as an input then i can do validation against XSD. But here i have a webmethod which takes JAXB object directly as an input. In such case validating input against XSD is whose responsibility.

Also, please suggest me as am using JAX-WS, my web method takes JAXB generated object as an input and not XML string. So which one is standard? Taking xml string as an input or JAXB object generated from XSD ? can i directly write a web method which accepts JAXB generated object?

Thanks!

By default, JAX-WS does not perform schema validation against incoming requests. You can enable it by adding @SchemaValidation to your service.

However, in answer to your closing question, your methods should definitely be accepting JAXB-generated objects as arguments. Accepting XML as a string is a very bad idea, as it indicates that it is not being validated or parsed. The whole point of using JAX-WS, Spring-WS, etc is to avoid messing around with the XML yourself. These libraries do it very well for you.

Following on from this, turning schema validation on is generally not necessary. This is because JAX-WS will be attempting to generate the arguments using a JAXB marshaller. If it can't generate those objects from the string of XML it receives, then they are clearly not valid, and it will throw an exception.

tl;dr - Yes you can validate the schema, but it's probably not worth doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM