About Question enthuware.ocejws.v6.2.86 :

Moderators: Site Manager, fjwalraven

Post Reply
himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

About Question enthuware.ocejws.v6.2.86 :

Post 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?

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

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

Post 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

austinor
Posts: 41
Joined: Mon Oct 27, 2014 11:35 pm
Contact:

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

Post 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 );

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

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

Post 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

austinor
Posts: 41
Joined: Mon Oct 27, 2014 11:35 pm
Contact:

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

Post 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.

ramy6_1
Posts: 124
Joined: Wed Feb 12, 2014 2:44 am
Contact:

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

Post 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 ?

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

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

Post by fjwalraven »

GetTable does not construct a SOAPMessage.

MESSAGE is only correct if you provide a full SOAPMessage.

Regards,
Frits

witek_m
Posts: 16
Joined: Sat Jun 09, 2018 12:09 pm
Contact:

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

Post by witek_m »

Hello, about last answer: Is it necessary to wrap getTable type with JAXBElement if getTable class has @XmlRootElement annotation?

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

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

Post by fjwalraven »

No, but I don't see that in the answers...

Regards,
Frits

Bespawler
Posts: 1
Joined: Sat Jan 12, 2019 3:54 am
Contact:

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

Post by Bespawler »

Hello, about last answer: Is it necessary to wrap getTable type with JAXBElement if getTable class has @XmlRootElement annotation?

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

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

Post by fjwalraven »

No, but I don't see that in the answers...

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests