How can I configure the target namespace globally in JAX-WS web services?
Asked Answered
I

1

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.

Id answered 4/6, 2013 at 10:52 Comment(0)
V
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

Voccola answered 4/6, 2013 at 20:49 Comment(3)
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. <ns0:locateCustomerResponse>), but the inner ones uses the default namespace (e.g. <CustomerRecord>).The same happens to the parameters (<firstName> instead of <ns0:firstName>).Id
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.Teece
It seems the easy way to to this is to rename the java package to match targetNamespace :(Felodese

© 2022 - 2024 — McMap. All rights reserved.