开发者

Publishing multiple ports with one service using JAX-WS 2.0 and @WebService

开发者 https://www.devze.com 2023-01-14 03:17 出处:网络
I want to create a SOAP service with multiple port types exposed, where each port type has a separate interface. I\'m trying to do this using JAX-WS 2.0.

I want to create a SOAP service with multiple port types exposed, where each port type has a separate interface. I'm trying to do this using JAX-WS 2.0.

Example:

interface A:
    ObjectA get(String name);

interface B:
    ObjectB get(String name);

Service:
    port A
          get
    port B
          get

The problem I'm having is that an @WebService is defined using a single class/interface, so the only way I'm able to set this up is having two separate services. Each service implemented by a separate class with a @WebService annotation.

I'd like to expose both ports using the same service, to make it clear that both of them are part of the same API. Is this possible?

Really, what I'm after is having some kind of nested namespace support in the WSDL, so I can have the same methods in different namespaces. I'll have get/set/delete methods for different kinds of data next to each other, but I'd rather not开发者_C百科 put them all in the same big interface with getA/getB and so on, since I'd like to be able to add new data types later on without forcing all the clients to regenerate from the new set of WSDLs. Any tips on achieving this are welcome, even if it means using another way of generating the WSDL from java code.


You may try to rename one of the methods and set the action or operationName fields in the @WebMethod annotation explicitly.


I would suggest instead of defining the input parameter as a String, you should consider defining a RequestType (a complextype in xsd), for each of those methods and that would give you the following advantages:
1. If you have a defined complex type, then the request can evolve independenty where you add more elements in the complex type, while the web method signature does not change in the wsdl.
2. You can have the same name for the 2 methods like u have above (say get(...)), while both of them will have the different request types. You can achieve this either by defining the two different elements in the xsd (with same namespace) with different request names. If you want to have the same name for the request elements, then you must consider defining them in different namespaces. That way in OOP, they will be generated in different packages and therefore they can have the same name.

On a different note, I would suggest that it is always good to have your operation names and the message names as unique and specific as possible.


Function overloading will not be accepted in Webservices. I mean multiple operation with same name can not be done. You need same port and same operation name to return different class Object, you can try the following.

interface

public interface OB {
public Object get(String name);
}

Webservice

@Override
@WebMethod
public Object get(String name) {
    if(name.equals("A")){
        return new ObjectA("A");
    }else if(name.equals("B")){
        return new ObjectB(1);
    }else {
        return null;
    }
}

In this case you have to identify which class object need to be returned from the request and then construct it and returning back is one of the way.

0

精彩评论

暂无评论...
验证码 换一张
取 消