简体   繁体   中英

Java Webservice with generic methods

I was wondering if it is possible to make a generic webservice method in java like this:

@WebMethod
public <T extends Foo> void testGeneric(T data){

However when I try to consume this with a Java client I get an error stating:

[ERROR] Schema descriptor { http://####/ }testGeneric in message part "parameters" is not defined and could not be bound to Java.

I know it is possible to make a method that takes a parameter such as List and this generates correctly using JAX-WS.

I don't mind if there is a solution that means I am tied to using only a particular technology.

Thanks, Dan.

I doubt it. But you can define a generic super interface and let your actual service interface extend it with parameters:

public interface BaseService<T>{
    T doWhackStuff();
}

public interface WhackyService extends BaseService<Whack>{
}

public interface EvenMoreWhackyService extends BaseService<Whackier>{
}

This approach usually only makes sense if you build your interfaces from multiple components:

public interface BaseService<T>{
    T doWhackStuff();
}

public interface ExtendedService<T>{
    T doMoreWhackStuff();
}

public interface DoubleWhackyService extends BaseService<Whack>, ExtendedService<Whack>{
}

I have not tested this approach in JAX-WS, but I know it works in Spring / GraniteDS / Flex

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