Search code examples
javaweb-serviceswsdlwebmethod

wsgen exposes methods that are not annotated with @WebMethod


I have the following minimal webservice defined:

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
public class DummyWS {

  public static void main(String[] args) {
    final String url= "http://localhost:8888/Dummy";
    final Endpoint endpoint= Endpoint.create(new DummyWS());
    endpoint.publish(url);
  }

  @WebMethod
  public void putData(final String value) {
    System.out.println("value: "+value);
  }

  @WebMethod
  public void doSomething() {
    System.out.println("doing nothing");
  }


  public void myInternalMethod() {
    System.out.println("should not be exposed via WSDL");
  }
}

As you can see I have two methods I want to expose, since they are annotated with @WebMethod: putData and doSomething. But when running wsgen, it generates a WSDL that contains the myInternalMethod although it is not annotated.

Do I have a misconfiguration here? Why is a method exposed that is not annotated with @WebMethod?


Solution

  • OK, I found it. Per default all public methods are exposed. To exclude a method one must annotate it with @WebMethod(exclude=true). This is quite a strange requirement, because it means that I only have to annotate those methods with @WebMethod that I do not want to expose.

    This is the correct code then:

    import javax.jws.WebMethod;
    import javax.jws.WebService;
    import javax.xml.ws.Endpoint;
    
    @WebService
    public class DummyWS {
    
      public static void main(String[] args) {
        final String url= "http://localhost:8888/Dummy";
        final Endpoint endpoint= Endpoint.create(new DummyWS());
        endpoint.publish(url);
      }
    
      public void putData(final String value) {
        System.out.println("value: "+value);
      }
    
      public void doSomething() {
        System.out.println("doing nothing");
      }
    
    
      @WebMethod(exclude=true)
      public void myInternalMethod() {
        System.out.println("should not be exposed via WSDL");
      }
    }