Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webservice with CXF: How to use the ResponseWrapper?

We are creating a webservice (CXF-based) driven by a java class (Java2WS) with the following method:

  @WebMethod
  @RequestWrapper(className = "com.myproject.wrapper.MyRequestWrapper")
  @ResponseWrapper(className = "com.myproject.wrapper.MyResponseWrapper")
  public MyResponse verifyCode(@WebParam(name = "code") String code) {
    ...
    return new MyResponse("Hello",StatusEnum.okay);
  }

I use the wrappers to define the elements of the request resp. response in more detail: the correct element names (which start with an uppercase character), required and optional elements, ...). But I am not sure if this is the right way to do it (there is no in-depth documentation about wrappers, isn't it?)

The class MyResponse:

public class MyResponseWrapper {

  private String result;   
  private ModeEnum status;

  // getters and setters
}

The class MyReponseWrapper

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "myResponse")
public class MyResponseWrapper {

  @XmlElement(name="Result")
  private String result;

  @XmlElement(name = "Status")
  private StatusEnum status;

  public MyResponseWrapper() {
    result="fu"; // just for testing
  }

  // getters and setters
}

Currently I don't understand the Wrappers. When I return an instance of MyReponse, how does the data from MyResponse be injected into MyResponseWrapper respectivly to the SOAP body of the response?

By testing this webservice I can see that an instance of MyResponseWrapper is instantiated and the SOAP body contains the correct elements but with default data (for example: result="fu" instead of "Hello"). I expected that CXF injects matching data from MyResponse to MyResponseWrapper. Is that wrong?

If this is the wrong way to do it: Wat is the right way to specify the resulting SOAP xml when using Java2WS?

By the way: The above source snippets are just examples taken from our more complex (more fields) classes.

like image 394
Bert Speckels Avatar asked Nov 20 '10 08:11

Bert Speckels


People also ask

What is the use of ResponseWrapper?

Annotation Type ResponseWrapper Used to annotate methods in the Service Endpoint Interface with the response wrapper bean to be used at runtime. The default value of the localName is the operationName as defined in WebMethod annotation appended with Response and the targetNamespace is the target namespace of the SEI.

What is Apache CXF used for?

Apache CXF™ is an open source services framework. CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI.

What is @ResponseWrapper in Java?

Specifies the local name of the wrapper element in the XML representation of the request message. The default value is the name of the method or the value of the @WebMethod annotation's operationName property. targetNamespace. Specifies the namespace under which the XML wrapper element is defined.


1 Answers

This is the right way to do it. The request and response wrappers just allow for overriding the xml namespace and element/attribute names for the request/response elements; respectively - which in turn map to the methods used to manage those values.

Ref: http://cxf.apache.org/docs/developing-a-service.html#DevelopingaService-The@RequestWrapperannotation

The @RequestWrapper annotation is defined by the javax.xml.ws.RequestWrapper interface. It is placed on the methods in the SEI. As the name implies, @RequestWrapper specifies the Java class that implements the wrapper bean for the method parameters that are included in the request message sent in a remote invocation. It is also used to specify the element names, and namespaces, used by the runtime when marshalling and unmarshalling the request messages.

The following table describes the properties of the @RequestWrapper annotation.

localName

Specifies the local name of the wrapper element in the XML representation of the request message. The default value is the name of the method or the value of the @WebMethod annotation's operationName property.

targetNamespace

Specifies the namespace under which the XML wrapper element is defined. The default value is the target namespace of the SEI.

className

Specifies the full name of the Java class that implements the wrapper element.

like image 117
Darrell Teague Avatar answered Nov 09 '22 21:11

Darrell Teague