Page 1 of 1

About Question enthuware.ocejws.v6.2.86 :

Posted: Tue Apr 08, 2014 2:25 pm
by himaiMinh
Hi, all mock exam users,
As there was a discussion in another forum, that service.createDispatch(portName,jaxbContext, Message.Mode.MESSAGE) will be very tricky. Most books provides examples that using jaxbcontext with Payload mode.

However, be aware that in MZ's notes, p. 95, there is a paragraph :
"JAXB objects may be used with any protocol binding in either message or message payload mode....Service provides a createDispatch factory method for creating Dispatch instances that contain an embedded JAXBContext. The context parameter contains the JAXBContext instance that the created Dispatch instance will use to marshall and unmarshall message or message payload."

Also, from Apache CXF JAX-WS dispatch API at https://cxf.apache.org/docs/jax-ws-dispatch-api.html,
there is a statement: "Message mode is not ideal when you wish to work with JAXB objects."

It seems that in createDispatch method, we are allowed to use jaxbContext with message mode or payload mode. But it will be tricky to implement that.

Any feedback?

Re: About Question enthuware.ocejws.v6.2.86 :

Posted: Tue Apr 08, 2014 11:01 pm
by fjwalraven
Hi Himai,

Please stick in these threads to questions about the mock exam questions.
However, be aware that in MZ's notes, p. 95, there is a paragraph :
Note that this can be found the JAX-WS specifications:
4.3 javax.xml.ws.Dispatch
JAXB Objects Use of JAXB allows clients to use JAXB objects generated from an XML Schema to create and manipulate XML representations and to use these objects with JAX-WS without requiring an intermediate XML serialization. JAXB objects may be used with any protocol binding in either message or message payload mode.
Regards,
Frits

Re: About Question enthuware.ocejws.v6.2.86 :

Posted: Tue Nov 04, 2014 12:51 pm
by austinor
For the question:

Code: Select all

We are creating a dispatch client against the following Web Service implementation. The Web Service's endpoint is published at the address "http://localhost:9999/math". The GetTable class has a @XmlRootElement annotation. What is the correct client code?  

package ws.math; 

@WebService 
public interface MathTableService {
    public SimpleMathTable getTable(Integer number) throws NegException; 
} 


package ws.math; 

@WebService (endpointInterface = "MathTableService") 
public class MathTableImpl {
    public SimpleMathTable getTable(Integer number) throws NegException {
       if (number < 0) {
          throw new NegException("Number cannot be negative");
       }
       SimpleMathTable table = new SimpleMathTable(number);
       return table;
    } 
}
The 1st of the 4 choices, with the explanation for why it is wrong, is:

Code: Select all

public class DispatchSoapMessageClient {

    private static final String Namespace = "http://math.ws/";

    public static void main(String[] args) throws Exception {
       URL url;
       try {
                url = new URL("http://localhost:9999/math?wsdl");
                QName serviceName = new QName(Namespace,"MathTableImplService");
                QName portName = new QName(Namespace,"MathTableImplPort");
                Service service = Service.create(url, serviceName);
                JAXBContext jaxbcontext = JAXBContext.newInstance( SOAPMessage.class );
                Dispatch<Object> dispatch = service.createDispatch(portName, jaxbcontext, Service.Mode.PAYLOAD);
                GetTable g = createRequestObject();
                JAXBElement<GetTableResponse> result = (JAXBElement<GetTableResponse>) dispatch.invoke( g );
       } catch (MalformedURLException e) {}
    }

    private static GetTable createRequestObject() {
       GetTable req = new GetTable();
       req.setArg0(new Integer(1));
       return req;
    } 
}

Explanation: The JAXBContext should include the request object.
Based on the above Explanation, would the following line make the above-coded client work... ?

Code: Select all

JAXBContext jaxbcontext = JAXBContext.newInstance( GetTable.class );

Re: About Question enthuware.ocejws.v6.2.86 :

Posted: Tue Nov 04, 2014 3:07 pm
by fjwalraven
Based on the above Explanation, would the following line make the above-coded client work... ?

Code:
JAXBContext jaxbcontext = JAXBContext.newInstance( GetTable.class );
Almost. The request can be sent to the Web Service (and the requested method will be executed) but you cannot handle the response object in your client code as the GetTableResponse is not known by JAXB. Apart from that it needs the ObjectFactory that is generated by wsimport.

Or in code:

Code: Select all

JAXBContext jaxbcontext = JAXBContext.newInstance(ws.client.GetTable.class, ws.client.GetTableResponse.class, ws.client.ObjectFactory.class);
The easier way to code this is to create a jaxbcontext like this:

Code: Select all

JAXBContext jaxbcontext = JAXBContext.newInstance("ws.client");
Assuming that you have the generated client artifacts in the "ws.client" package.

Regards,
Frits

Re: About Question enthuware.ocejws.v6.2.86 :

Posted: Thu Nov 06, 2014 7:04 pm
by austinor
Ok, now I get it.

The examples I've coded were passing the package name/s to the JAXBContext during its creation.

Now that I see that the request, response and ObjectFactory classes are explicitly passed to the JAXBContext, the point is now clearer to me. Thanks again.

Re: About Question enthuware.ocejws.v6.2.86 :

Posted: Wed Nov 11, 2015 5:59 am
by ramy6_1
Hello ,

"You are providing the GetTable object as PAYLOAD to the SOAPMessage, that is why MESSAGE is wrong."

What indicates that you send GetTable to PAYLOAD ?
And also Message includes the Payload , so why MESSAGE is wrong ?

Re: About Question enthuware.ocejws.v6.2.86 :

Posted: Wed Nov 11, 2015 8:03 am
by fjwalraven
GetTable does not construct a SOAPMessage.

MESSAGE is only correct if you provide a full SOAPMessage.

Regards,
Frits

Re: About Question enthuware.ocejws.v6.2.86 :

Posted: Sun Jun 17, 2018 4:53 pm
by witek_m
Hello, about last answer: Is it necessary to wrap getTable type with JAXBElement if getTable class has @XmlRootElement annotation?

Re: About Question enthuware.ocejws.v6.2.86 :

Posted: Wed Jun 20, 2018 12:37 am
by fjwalraven
No, but I don't see that in the answers...

Regards,
Frits

Re: About Question enthuware.ocejws.v6.2.86 :

Posted: Sat Apr 13, 2019 6:05 am
by Bespawler
Hello, about last answer: Is it necessary to wrap getTable type with JAXBElement if getTable class has @XmlRootElement annotation?

Re: About Question enthuware.ocejws.v6.2.86 :

Posted: Sat Apr 13, 2019 6:27 am
by fjwalraven
No, but I don't see that in the answers...