1

I am trying to create Webservice Simulator where we can create SOAP Webservices by just providing the .xsd or xml file. After providing xml/xsd wsdl file will be generated and for that reason only i am trying to make ServiceEndPoint class methods generic so that single method provides response for all operations.

Till now i have made sample code to test how soap web service is generated using spring web service and uses JAXP APIs. I want to make following method generic so that it provides response to all operations :

@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getStudentRequest")
@ResponsePayload
public GetStudentResponse getCountry(@RequestPayload GetStudentRequest request) {
    GetStudentResponse response = new GetStudentResponse();
    response.setStudent(studentUtility.getStudent(request.getStudentId()));
    return response;

As of now above method is binded to specific operation getStudentRequest Please help me to know how can I make above method generic so that it provides response for all operations.

Please find below the xsd file through which I am generating WSDL file:

<xs:element name="getStudentRequest">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="studentId" type="xs:int"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:element name="getStudentResponse">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="student" type="tns:student"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:complexType name="student">
    <xs:sequence>
        <xs:element name="studentId" type="xs:int"/>
        <xs:element name="name" type="xs:string"/>
        <xs:element name="age" type="xs:int"/>
        <xs:element name="class" type="xs:string"/>
    </xs:sequence>
</xs:complexType>

BlackM
  • 3,927
  • 8
  • 39
  • 69
user3403462
  • 121
  • 2
  • 6

3 Answers3

0

You can try using generics in your method like below:

public <T extends IRequest, R extends IResponse> R getCountry(@RequestPayload T request) {

To make this work, the coresponding request and response must be sub classed.

class GetStudentRequest implements IRequest{}
class GetStudentResponse implements IResponse{}

If yor are generating code using apache cxf or jaxb, this can be done by providing JAXB custom binding with inheritance plugin to create request/response classes with the interface implemented.

for eg.

<inheritance:implements>mypackage.IRequest</inheritance:implements> 
Sridhar
  • 1,832
  • 3
  • 23
  • 44
  • Thanks for the above solution, i have added the jabxb2-maven- plugin in maven and please find below the dependency added .Please let me know where i have to add the `mypackage.IRequest` this line – user3403462 Jun 19 '15 at 10:03
  • `org.codehaus.mojo jaxb2-maven-plugin 1.6 xjc xjc ${project.basedir}/src/main/resources/ ${project.basedir}/src/main/java false ` – user3403462 Jun 19 '15 at 10:05
  • please refer http://stackoverflow.com/questions/1271980/generating-a-jaxb-class-that-implements-an-interface and http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.5/tutorial/doc/JAXBUsing4.html – Sridhar Jun 19 '15 at 10:16
0

You cannot, WSDL doesn't support Generic Types to be Passed across. The reason for wsdl is basically used to inform the client about the methods and types, the client can generate proxy using the wsdl and your types will be strongly typed.

If you still want to do it, it defeats the purpose of wsdl. you can serialise and pass as xml or any string and parse/deserialise back in the client. obviously the client will not know about this type, which doesnt make any sense.

Sathya
  • 9
  • 1
0

If you want to do this: getCountry, getStudent, getX, getY actions (which have same input/output signature) should all point to single web service function you implemented, below hack can be solution:

If you use CXF (or similar lib), you can use interceptors for manipulating your requests headers and content. If you register this interceptor to a proper place like "READ" (see phases list), it'd be possible to change header: SOAPAction value from getX() to getCountry().

Also see this question: Stackoverflow: Apache CXF - Set HTTP header

Community
  • 1
  • 1
hsnkhrmn
  • 961
  • 7
  • 20