7

I have a lot of endpoints annotated with @WebService(targetNamespace = "mynamespace"). Every @WebResult and @WebParam has the same definition of targetNamespace = "mynamespace".

Is there a way to configure JAX-WS (Metro implementation) to use "mynamespace" as targetNamespace by default?

I would like to use the annotations without any attributes and get rid of the duplicate declarations, just like convention over configuration.

timomeinen
  • 3,101
  • 3
  • 33
  • 46

1 Answers1

3

Only put the targetNamespace in the service endpoint interface or service implementation bean.

/**
* Annotated Implementation Object
*/
@WebService(
    name = "CustomerService",
    targetNamespace = "http://org.company.services"
)
public class CustomerService {
    @WebMethod
    @WebResult(name="CustomerRecord")
    public CustomerRecord locateCustomer(
        @WebParam(name="FirstName") String firstName,
        @WebParam(name="LastName") String lastName,
        @WebParam(name="Address") USAddress addr) {
        ...
    }
};

If @WebResult or @WebParam have no targetNamespace, the default is the targetNamespace for the Web Service.

In another hand, you can avoid the all annotations and only use the @WebService if you don't need something custom with JAX-B.

See more in JSR-181 Web Services Metadata for the JavaTM Platform

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
  • 8
    When I leave out the `targetNamespace` at `@WebResult` and at `@WebParam` the generated XML in the SOAP response has a namespace for the outermost XML tag (e.g. ``), but the inner ones uses the default namespace (e.g. ``).The same happens to the parameters (`` instead of ``). – timomeinen Jun 05 '13 at 06:45
  • One would think the above example would work, but unfortunately it doesn't like tim mentions above. Why is something so simple not easy to achieve? There has to be a way to not constantly copy and paste this namespace on every single WebParam and WebResult annotation. – GreenieMeanie Jun 28 '16 at 17:05
  • It seems the easy way to to this is to rename the java package to match targetNamespace :( – Toilal Aug 06 '20 at 12:27