You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by amathewcxf <am...@tickets.com> on 2012/05/22 22:43:32 UTC
How to use XSD file with cxf
Hi,
I am a new user for cxf. We are trying to develop a webservice client
based on a xsd file. I was able to create domain classes from xsd file using
Eclipse. Now i wanted to call the remote API using this webservice client. I
am not sure how the cxf will be able to make use of my domain classes which
i generated from the XSD. The remote API is a REST-based application and
hence my request will have a querystring with the necessary data and the
response will be in JSON format.
In fact i read lot of documents, but i couldn't get a real picture. So
any thoughts will be very useful for me.
Thanks
Anil Mathew
--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-use-XSD-file-with-cxf-tp5708311.html
Sent from the cxf-user mailing list archive at Nabble.com.
RE: How to use XSD file with cxf
Posted by Abhishek Sharma <Ab...@makemytrip.com>.
Thanks for the help. :)
-----Original Message-----
From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
Sent: Monday, June 11, 2012 5:28 PM
To: users@cxf.apache.org
Cc: Abhishek Sharma
Subject: Re: How to use XSD file with cxf
Hi Abhishek
On 09/06/12 07:40, Abhishek Sharma wrote:
> I Appreciate your efforts in getting it done and sharing the same :)
>
> One Q related to MappedNamespaceConvention provider:
>
> I used Jackson provider explicitly as I was getting some problem with the default json provider in CXF as it was creating an unnecessary wrapper for the root element.
>
> So when you publish your schema the json may or may not contain additional wrapper.
>
> Which one is syntactically correct as different parsers have different behaviors?
>
> For e.g.:
> Sample XML:
>
> <MMTHotelAvailRequest>
> <POS>
> <Requestor type="B2BAgent" idContext="FPH" id="AFF0987" channel="B2C"/>
> <Source iSOCurrency="INR"/>
> </POS>
> </MMTHotelAvailRequest>
>
> JSON1 (thru CXF):
> {
> "MMTHotelAvailRequest": {
> "POS": {
> "Requestor": {
> "type": "B2BAgent",
> "idContext": "FPH",
> "id": "AFF0987",
> "channel": "B2C"
> },
> "Source": { "iSOCurrency": "INR" }
> }
> }
> }
>
> JSON2 (thru Jackson):
> {
> "POS" : {
> "Requestor" : {
> "channel" : "Mobile",
> "id" : "AFF0987",
> "idContext" : "MMT",
> "type" : "MobileAgent"
> },
> "Source" : {"iSOCurrency" : "INR"
> }
> }
> }
Jettison is JAXB-driven so it may not be able to produce optimal JSON
sequences; despite that, one advantage of being JAXB-driven is that it
is quite configurable. Modifying, validating and restricting JSON
sequences are easy with Jettison. One option is to configure it to drop
the "POS" element, see
http://cxf.apache.org/docs/transformationfeature.html#TransformationFeature-Droppingoutputandinputelements
Use an outDropElements property
HTH, Sergey
>
> Thanks,
> Abhishek
>
> -----Original Message-----
> From: amathewcxf [mailto:amathew@tickets.com]
> Sent: Saturday, June 09, 2012 12:02 AM
> To: users@cxf.apache.org
> Subject: RE: How to use XSD file with cxf
>
> Thank you Abhishek for all the help. I have it all working with WebClient&
> JSON as the type.
>
> Below is my final client code (I am posting so it may help others in the
> forum):
>
> try {
> WebClient webClient =
> WebClient.create("https://myremotws.com:18443/availability/v1?eventId=1146",
> Collections.singletonList(new MappedNamespaceConvention(new
> Configuration())));
> String authorizationHeader = "Basic " +
> org.apache.cxf.common.util.Base64Utility.encode("myusername:mypassword".getBytes());
> webClient.header("Authorization", authorizationHeader);
> webClient.accept(MediaType.APPLICATION_JSON);
>
> // This section of code will make sure client is treated as a Trusted
> source by remote service
> // and this is needed only if the remote server doesnt have a valid
> certificate
> HTTPConduit conduit = WebClient.getConfig(webClient).getHttpConduit();
> conduit.setTlsClientParameters(getConfiguredClientSSLParms(webClient));
>
> // This will make the exact remote webservice all to get the response
> // The response data will be JSON String and the above provider
> (MappedNamespaceConvention) which
> // we registered will take care of all JSON parsing to our JAXB classes.
> Availability avail = webClient.get(Availability.class);
>
> } catch (Throwable e) {
>
> }
>
> Thanks
> Anil Mathew
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/How-to-use-XSD-file-with-cxf-tp5708311p5709415.html
> Sent from the cxf-user mailing list archive at Nabble.com.
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com
Re: How to use XSD file with cxf
Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi Abhishek
On 09/06/12 07:40, Abhishek Sharma wrote:
> I Appreciate your efforts in getting it done and sharing the same :)
>
> One Q related to MappedNamespaceConvention provider:
>
> I used Jackson provider explicitly as I was getting some problem with the default json provider in CXF as it was creating an unnecessary wrapper for the root element.
>
> So when you publish your schema the json may or may not contain additional wrapper.
>
> Which one is syntactically correct as different parsers have different behaviors?
>
> For e.g.:
> Sample XML:
>
> <MMTHotelAvailRequest>
> <POS>
> <Requestor type="B2BAgent" idContext="FPH" id="AFF0987" channel="B2C"/>
> <Source iSOCurrency="INR"/>
> </POS>
> </MMTHotelAvailRequest>
>
> JSON1 (thru CXF):
> {
> "MMTHotelAvailRequest": {
> "POS": {
> "Requestor": {
> "type": "B2BAgent",
> "idContext": "FPH",
> "id": "AFF0987",
> "channel": "B2C"
> },
> "Source": { "iSOCurrency": "INR" }
> }
> }
> }
>
> JSON2 (thru Jackson):
> {
> "POS" : {
> "Requestor" : {
> "channel" : "Mobile",
> "id" : "AFF0987",
> "idContext" : "MMT",
> "type" : "MobileAgent"
> },
> "Source" : {"iSOCurrency" : "INR"
> }
> }
> }
Jettison is JAXB-driven so it may not be able to produce optimal JSON
sequences; despite that, one advantage of being JAXB-driven is that it
is quite configurable. Modifying, validating and restricting JSON
sequences are easy with Jettison. One option is to configure it to drop
the "POS" element, see
http://cxf.apache.org/docs/transformationfeature.html#TransformationFeature-Droppingoutputandinputelements
Use an outDropElements property
HTH, Sergey
>
> Thanks,
> Abhishek
>
> -----Original Message-----
> From: amathewcxf [mailto:amathew@tickets.com]
> Sent: Saturday, June 09, 2012 12:02 AM
> To: users@cxf.apache.org
> Subject: RE: How to use XSD file with cxf
>
> Thank you Abhishek for all the help. I have it all working with WebClient&
> JSON as the type.
>
> Below is my final client code (I am posting so it may help others in the
> forum):
>
> try {
> WebClient webClient =
> WebClient.create("https://myremotws.com:18443/availability/v1?eventId=1146",
> Collections.singletonList(new MappedNamespaceConvention(new
> Configuration())));
> String authorizationHeader = "Basic " +
> org.apache.cxf.common.util.Base64Utility.encode("myusername:mypassword".getBytes());
> webClient.header("Authorization", authorizationHeader);
> webClient.accept(MediaType.APPLICATION_JSON);
>
> // This section of code will make sure client is treated as a Trusted
> source by remote service
> // and this is needed only if the remote server doesnt have a valid
> certificate
> HTTPConduit conduit = WebClient.getConfig(webClient).getHttpConduit();
> conduit.setTlsClientParameters(getConfiguredClientSSLParms(webClient));
>
> // This will make the exact remote webservice all to get the response
> // The response data will be JSON String and the above provider
> (MappedNamespaceConvention) which
> // we registered will take care of all JSON parsing to our JAXB classes.
> Availability avail = webClient.get(Availability.class);
>
> } catch (Throwable e) {
>
> }
>
> Thanks
> Anil Mathew
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/How-to-use-XSD-file-with-cxf-tp5708311p5709415.html
> Sent from the cxf-user mailing list archive at Nabble.com.
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com
RE: How to use XSD file with cxf
Posted by Abhishek Sharma <Ab...@makemytrip.com>.
I Appreciate your efforts in getting it done and sharing the same :)
One Q related to MappedNamespaceConvention provider:
I used Jackson provider explicitly as I was getting some problem with the default json provider in CXF as it was creating an unnecessary wrapper for the root element.
So when you publish your schema the json may or may not contain additional wrapper.
Which one is syntactically correct as different parsers have different behaviors?
For e.g.:
Sample XML:
<MMTHotelAvailRequest>
<POS>
<Requestor type="B2BAgent" idContext="FPH" id="AFF0987" channel="B2C"/>
<Source iSOCurrency="INR"/>
</POS>
</MMTHotelAvailRequest>
JSON1 (thru CXF):
{
"MMTHotelAvailRequest": {
"POS": {
"Requestor": {
"type": "B2BAgent",
"idContext": "FPH",
"id": "AFF0987",
"channel": "B2C"
},
"Source": { "iSOCurrency": "INR" }
}
}
}
JSON2 (thru Jackson):
{
"POS" : {
"Requestor" : {
"channel" : "Mobile",
"id" : "AFF0987",
"idContext" : "MMT",
"type" : "MobileAgent"
},
"Source" : {"iSOCurrency" : "INR"
}
}
}
Thanks,
Abhishek
-----Original Message-----
From: amathewcxf [mailto:amathew@tickets.com]
Sent: Saturday, June 09, 2012 12:02 AM
To: users@cxf.apache.org
Subject: RE: How to use XSD file with cxf
Thank you Abhishek for all the help. I have it all working with WebClient &
JSON as the type.
Below is my final client code (I am posting so it may help others in the
forum):
try {
WebClient webClient =
WebClient.create("https://myremotws.com:18443/availability/v1?eventId=1146",
Collections.singletonList(new MappedNamespaceConvention(new
Configuration())));
String authorizationHeader = "Basic " +
org.apache.cxf.common.util.Base64Utility.encode("myusername:mypassword".getBytes());
webClient.header("Authorization", authorizationHeader);
webClient.accept(MediaType.APPLICATION_JSON);
// This section of code will make sure client is treated as a Trusted
source by remote service
// and this is needed only if the remote server doesnt have a valid
certificate
HTTPConduit conduit = WebClient.getConfig(webClient).getHttpConduit();
conduit.setTlsClientParameters(getConfiguredClientSSLParms(webClient));
// This will make the exact remote webservice all to get the response
// The response data will be JSON String and the above provider
(MappedNamespaceConvention) which
// we registered will take care of all JSON parsing to our JAXB classes.
Availability avail = webClient.get(Availability.class);
} catch (Throwable e) {
}
Thanks
Anil Mathew
--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-use-XSD-file-with-cxf-tp5708311p5709415.html
Sent from the cxf-user mailing list archive at Nabble.com.
RE: How to use XSD file with cxf
Posted by amathewcxf <am...@tickets.com>.
Thank you Abhishek for all the help. I have it all working with WebClient &
JSON as the type.
Below is my final client code (I am posting so it may help others in the
forum):
try {
WebClient webClient =
WebClient.create("https://myremotws.com:18443/availability/v1?eventId=1146",
Collections.singletonList(new MappedNamespaceConvention(new
Configuration())));
String authorizationHeader = "Basic " +
org.apache.cxf.common.util.Base64Utility.encode("myusername:mypassword".getBytes());
webClient.header("Authorization", authorizationHeader);
webClient.accept(MediaType.APPLICATION_JSON);
// This section of code will make sure client is treated as a Trusted
source by remote service
// and this is needed only if the remote server doesnt have a valid
certificate
HTTPConduit conduit = WebClient.getConfig(webClient).getHttpConduit();
conduit.setTlsClientParameters(getConfiguredClientSSLParms(webClient));
// This will make the exact remote webservice all to get the response
// The response data will be JSON String and the above provider
(MappedNamespaceConvention) which
// we registered will take care of all JSON parsing to our JAXB classes.
Availability avail = webClient.get(Availability.class);
} catch (Throwable e) {
}
Thanks
Anil Mathew
--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-use-XSD-file-with-cxf-tp5708311p5709415.html
Sent from the cxf-user mailing list archive at Nabble.com.
RE: How to use XSD file with cxf
Posted by Abhishek Sharma <Ab...@makemytrip.com>.
Hi,
You can set the provider in JAXRSClientFactoryBean. Below is the code snippet I used in my code:
JAXRSClientFactoryBean factoryBean = new JAXRSClientFactoryBean();
factoryBean.setResourceClass(MobileHotelService.class);
factoryBean.setAddress(url);
factoryBean.setProvider(new MobileJAXBProvider());
interfaze = factoryBean.create(MobileHotelService.class);
org.apache.cxf.jaxrs.client.Client client = WebClient.client(interfaze);
client.accept("application/xml");
client.type("application/xml");
client.header("username", username);
client.header("authtoken", authtoken);
Regards,
Abhishek
-----Original Message-----
From: amathewcxf [mailto:amathew@tickets.com]
Sent: Wednesday, June 06, 2012 6:16 AM
To: users@cxf.apache.org
Subject: RE: How to use XSD file with cxf
The good news is I was able to get the response from the remote service by
using the following code:
@Test
public void testJSONGetAvailability() throws Exception {
if (this.getAvailabilityService != null) {
try {
WebClient webClient =
WebClient.create("https://myremotews/v1?eventId=1146", "username",
"password", null);
webClient.accept(MediaType.APPLICATION_JSON);
HTTPConduit conduit = WebClient.getConfig(webClient).getHttpConduit();
conduit.setTlsClientParameters(getConfiguredClientSSLParms(webClient));
Response res = webClient.get();
* InputStream resIs = (InputStream) res.getEntity();
Object availObj = unmarshallJSONFile(resIs);*
if (availObj != null) {
if (availObj instanceof Availability) {
Availability avail = (Availability) availObj;
// I AM GETTING THE AVAILABILITY OBJECT HERE!!!
}
}
} catch (Throwable e) {
System.out.println("Error in testGetAvailability:" + e);
}
}
}
----
Below is the unmarshallJSONFile method:
public Object unmarshallJSONFile(InputStream is) {
String response = "";
try {
JAXBContext jc = JAXBContext.newInstance(Availability.class);
InputStreamReader sReader = new InputStreamReader(is);
LineNumberReader reader = new LineNumberReader(sReader);
StringWriter strWriter = new StringWriter();
PrintWriter writer = new PrintWriter(strWriter);
String str = reader.readLine();
while (str != null) {
if (str.length() > 0) {
writer.println(str);
}
str = reader.readLine();
}
writer.flush();
response = strWriter.toString();
JSONObject jsonObject = new JSONObject(response);
Configuration config = new Configuration();
MappedNamespaceConvention con = new
MappedNamespaceConvention(config);
XMLStreamReader xmlStreamReader = new
MappedXMLStreamReader(jsonObject, con);
Unmarshaller unmarshaller = jc.createUnmarshaller();
return unmarshaller.unmarshal(xmlStreamReader);
} catch (Throwable e) {
}
return null;
}
----
But I don't wantto do unmarshallJSONFile coding as I wanted to use
"JacksonJsonProvider" assuming that it will do the same for me. But with
JacksonJsonProvider, i am not able to send the username/password as I don't
see a WebClient.create method which takes the BaseURI, username, password,
providerList. So i wonder how do i do this? Any thoughts will help me.
Thanks for the help
Anil Mathew
--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-use-XSD-file-with-cxf-tp5708311p5709160.html
Sent from the cxf-user mailing list archive at Nabble.com.
Re: How to use XSD file with cxf
Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
JAXRSClientFactory is a utitity class which simplifies creating proxies
by providing a number of factory methods, but it is difficult to provide
the factory methods for all the possible combinations.
When JAXRSClientFactory has no the right method, then simply use
JAXRSClientFactoryBean:
JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
bean.setAddress("http://foo");
bean.setProviders(myProviders);
WebClient wc = bean.createWebClient();
HTH, Sergey
On 06/06/12 01:46, amathewcxf wrote:
> The good news is I was able to get the response from the remote service by
> using the following code:
>
> @Test
> public void testJSONGetAvailability() throws Exception {
>
> if (this.getAvailabilityService != null) {
> try {
> WebClient webClient =
> WebClient.create("https://myremotews/v1?eventId=1146", "username",
> "password", null);
> webClient.accept(MediaType.APPLICATION_JSON);
>
> HTTPConduit conduit = WebClient.getConfig(webClient).getHttpConduit();
> conduit.setTlsClientParameters(getConfiguredClientSSLParms(webClient));
> Response res = webClient.get();
>
> * InputStream resIs = (InputStream) res.getEntity();
>
> Object availObj = unmarshallJSONFile(resIs);*
> if (availObj != null) {
> if (availObj instanceof Availability) {
> Availability avail = (Availability) availObj;
> // I AM GETTING THE AVAILABILITY OBJECT HERE!!!
> }
> }
> } catch (Throwable e) {
> System.out.println("Error in testGetAvailability:" + e);
> }
>
> }
> }
>
> ----
> Below is the unmarshallJSONFile method:
>
> public Object unmarshallJSONFile(InputStream is) {
>
> String response = "";
> try {
> JAXBContext jc = JAXBContext.newInstance(Availability.class);
>
> InputStreamReader sReader = new InputStreamReader(is);
> LineNumberReader reader = new LineNumberReader(sReader);
> StringWriter strWriter = new StringWriter();
> PrintWriter writer = new PrintWriter(strWriter);
> String str = reader.readLine();
> while (str != null) {
> if (str.length()> 0) {
> writer.println(str);
> }
> str = reader.readLine();
> }
> writer.flush();
> response = strWriter.toString();
> JSONObject jsonObject = new JSONObject(response);
>
> Configuration config = new Configuration();
> MappedNamespaceConvention con = new
> MappedNamespaceConvention(config);
> XMLStreamReader xmlStreamReader = new
> MappedXMLStreamReader(jsonObject, con);
> Unmarshaller unmarshaller = jc.createUnmarshaller();
> return unmarshaller.unmarshal(xmlStreamReader);
>
> } catch (Throwable e) {
>
> }
>
> return null;
> }
>
> ----
>
> But I don't wantto do unmarshallJSONFile coding as I wanted to use
> "JacksonJsonProvider" assuming that it will do the same for me. But with
> JacksonJsonProvider, i am not able to send the username/password as I don't
> see a WebClient.create method which takes the BaseURI, username, password,
> providerList. So i wonder how do i do this? Any thoughts will help me.
>
> Thanks for the help
>
> Anil Mathew
>
>
>
>
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/How-to-use-XSD-file-with-cxf-tp5708311p5709160.html
> Sent from the cxf-user mailing list archive at Nabble.com.
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com
RE: How to use XSD file with cxf
Posted by amathewcxf <am...@tickets.com>.
The good news is I was able to get the response from the remote service by
using the following code:
@Test
public void testJSONGetAvailability() throws Exception {
if (this.getAvailabilityService != null) {
try {
WebClient webClient =
WebClient.create("https://myremotews/v1?eventId=1146", "username",
"password", null);
webClient.accept(MediaType.APPLICATION_JSON);
HTTPConduit conduit = WebClient.getConfig(webClient).getHttpConduit();
conduit.setTlsClientParameters(getConfiguredClientSSLParms(webClient));
Response res = webClient.get();
* InputStream resIs = (InputStream) res.getEntity();
Object availObj = unmarshallJSONFile(resIs);*
if (availObj != null) {
if (availObj instanceof Availability) {
Availability avail = (Availability) availObj;
// I AM GETTING THE AVAILABILITY OBJECT HERE!!!
}
}
} catch (Throwable e) {
System.out.println("Error in testGetAvailability:" + e);
}
}
}
----
Below is the unmarshallJSONFile method:
public Object unmarshallJSONFile(InputStream is) {
String response = "";
try {
JAXBContext jc = JAXBContext.newInstance(Availability.class);
InputStreamReader sReader = new InputStreamReader(is);
LineNumberReader reader = new LineNumberReader(sReader);
StringWriter strWriter = new StringWriter();
PrintWriter writer = new PrintWriter(strWriter);
String str = reader.readLine();
while (str != null) {
if (str.length() > 0) {
writer.println(str);
}
str = reader.readLine();
}
writer.flush();
response = strWriter.toString();
JSONObject jsonObject = new JSONObject(response);
Configuration config = new Configuration();
MappedNamespaceConvention con = new
MappedNamespaceConvention(config);
XMLStreamReader xmlStreamReader = new
MappedXMLStreamReader(jsonObject, con);
Unmarshaller unmarshaller = jc.createUnmarshaller();
return unmarshaller.unmarshal(xmlStreamReader);
} catch (Throwable e) {
}
return null;
}
----
But I don't wantto do unmarshallJSONFile coding as I wanted to use
"JacksonJsonProvider" assuming that it will do the same for me. But with
JacksonJsonProvider, i am not able to send the username/password as I don't
see a WebClient.create method which takes the BaseURI, username, password,
providerList. So i wonder how do i do this? Any thoughts will help me.
Thanks for the help
Anil Mathew
--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-use-XSD-file-with-cxf-tp5708311p5709160.html
Sent from the cxf-user mailing list archive at Nabble.com.
RE: How to use XSD file with cxf
Posted by Abhishek Sharma <Ab...@makemytrip.com>.
The problem is you have registered jacksonjson parser but the content type you are sending is text/html.
One solution could be set the appropriate Accept header and content type:
webClient.accept(MediaType.APPLICATION_JSON);
webClient.type(MediaType.APPLICATION_JSON);
And if you want to do the same using xml then register jaxb parser and set content type to application/xml.
webClient.accept MediaType.APPLICATION_XML);
webClient.type MediaType.APPLICATION_XML);
Try this out and please let me know the results.
Regards,
Abhishek
-----Original Message-----
From: amathewcxf [mailto:amathew@tickets.com]
Sent: Tuesday, June 05, 2012 6:32 AM
To: users@cxf.apache.org
Subject: RE: How to use XSD file with cxf
Thank You Abhishek. Very much appreciated your time/reply.
I was able to move forward quiet a bit. Below are my classes now:
Class 1: GetAvailabilityTest => A JUNIT client file
public class GetAvailabilityTest {
@Autowired
private GetAvailabilityService getAvailabilityService;
@Test
public void testGetAvailability() throws Exception {
if(this.getAvailabilityService != null){
try{
WebClient webClient = WebClient.create("https://mywsremoteurl:8080",
Collections.singletonList(new JacksonJsonProvider()));
HTTPConduit conduit =
WebClient.getConfig(webClient).getHttpConduit();
conduit.setTlsClientParameters(getConfiguredClientSSLParms(webClient));
this.getAvailabilityService.getAvailability(webClient, 1146);
}catch(Throwable e){
System.out.println("Error in testGetAvailability:" + e);
}
}
}
protected TLSClientParameters getConfiguredClientSSLParms(WebClient
client) {
TLSClientParameters params =
WebClient.getConfig(client).getHttpConduit().getTlsClientParameters();
TrustManager[] trustAllCerts = new TrustManager[] { new
X509TrustManager() {
public java.security.cert.X509Certificate[]
getAcceptedIssuers() {
return null;
}
public void
checkClientTrusted(java.security.cert.X509Certificate[] certs, String
authType) {
}
public void
checkServerTrusted(java.security.cert.X509Certificate[] certs, String
authType) {
}
} };
if (params == null) {
params = new TLSClientParameters();
params.setDisableCNCheck(true);
params.setSecureSocketProtocol("SSL");
}
params.setTrustManagers(trustAllCerts);
return params;
}
}
--------------------------------------
Class 2: GetAvailabilityServiceImpl.java
@Path("/ws-api/availability/v1")
public class GetAvailabilityServiceImpl implements GetAvailabilityService{
@GET
public Availability getAvailability(WebClient webClient,
@QueryParam("eventId") int eventId){
Availability avail = webClient.get(Availability.class);
return avail;
}
}
-------------------
Interface - GetAvailabilityService.java
@Path("/ws-api/availability/v1")
public interface GetAvailabilityService {
@GET
public Availability getAvailability(WebClient webClient,
@QueryParam("eventId") int eventId);
}
----
Class which i generated from xsd using JAXB
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "availability", propOrder = {
"event",
"groups"
})
@XmlRootElement
public class Availability {
protected SimpleDomainBean event;
protected Groups groups;
/**
* Gets the value of the event property.
*
* @return
* possible object is
* {@link SimpleDomainBean }
*
*/
public SimpleDomainBean getEvent() {
return event;
}
/**
* Sets the value of the event property.
*
* @param value
* allowed object is
* {@link SimpleDomainBean }
*
*/
public void setEvent(SimpleDomainBean value) {
this.event = value;
}
/**
* Gets the value of the groups property.
*
* @return
* possible object is
* {@link Groups }
*
*/
public Groups getGroups() {
return groups;
}
/**
* Sets the value of the groups property.
*
* @param value
* allowed object is
* {@link Groups }
*
*/
public void setGroups(Groups value) {
this.groups = value;
}
}
When i run this JUNIT test client, I am getting the below error:
INFO: Problem with setting the default provider
org.apache.cxf.jaxrs.provider.JSONProviderorg/codehaus/jettison/mapped/TypeConverter
Jun 4, 2012 5:50:23 PM org.apache.cxf.jaxrs.provider.ProviderFactory
createProvider
INFO: Problem with setting the default provider
org.apache.cxf.jaxrs.provider.JSONProviderorg/codehaus/jettison/mapped/TypeConverter
Jun 4, 2012 5:50:29 PM org.apache.cxf.jaxrs.client.AbstractClient
reportMessageHandlerProblem
SEVERE: .No message body reader has been found for class : class
com.tickets.ws.availability.Availability, ContentType :
text/html;charset=utf-8.
Error in
testGetAvailability:org.apache.cxf.jaxrs.client.ClientWebApplicationException:
.No message body reader has been found for class : class
com.tickets.ws.availability.Availability, ContentType :
text/html;charset=utf-8.
You may notice that I am alreading including the JacksonJsonProvider in the
WebClient.create method and also i have the @XmlRootElement in the
Availability.java.
Any thoughts on this will be helpful to me.
Thank You
Anil Mathew
--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-use-XSD-file-with-cxf-tp5708311p5709079.html
Sent from the cxf-user mailing list archive at Nabble.com.
RE: How to use XSD file with cxf
Posted by amathewcxf <am...@tickets.com>.
Thank You Abhishek. Very much appreciated your time/reply.
I was able to move forward quiet a bit. Below are my classes now:
Class 1: GetAvailabilityTest => A JUNIT client file
public class GetAvailabilityTest {
@Autowired
private GetAvailabilityService getAvailabilityService;
@Test
public void testGetAvailability() throws Exception {
if(this.getAvailabilityService != null){
try{
WebClient webClient = WebClient.create("https://mywsremoteurl:8080",
Collections.singletonList(new JacksonJsonProvider()));
HTTPConduit conduit =
WebClient.getConfig(webClient).getHttpConduit();
conduit.setTlsClientParameters(getConfiguredClientSSLParms(webClient));
this.getAvailabilityService.getAvailability(webClient, 1146);
}catch(Throwable e){
System.out.println("Error in testGetAvailability:" + e);
}
}
}
protected TLSClientParameters getConfiguredClientSSLParms(WebClient
client) {
TLSClientParameters params =
WebClient.getConfig(client).getHttpConduit().getTlsClientParameters();
TrustManager[] trustAllCerts = new TrustManager[] { new
X509TrustManager() {
public java.security.cert.X509Certificate[]
getAcceptedIssuers() {
return null;
}
public void
checkClientTrusted(java.security.cert.X509Certificate[] certs, String
authType) {
}
public void
checkServerTrusted(java.security.cert.X509Certificate[] certs, String
authType) {
}
} };
if (params == null) {
params = new TLSClientParameters();
params.setDisableCNCheck(true);
params.setSecureSocketProtocol("SSL");
}
params.setTrustManagers(trustAllCerts);
return params;
}
}
--------------------------------------
Class 2: GetAvailabilityServiceImpl.java
@Path("/ws-api/availability/v1")
public class GetAvailabilityServiceImpl implements GetAvailabilityService{
@GET
public Availability getAvailability(WebClient webClient,
@QueryParam("eventId") int eventId){
Availability avail = webClient.get(Availability.class);
return avail;
}
}
-------------------
Interface - GetAvailabilityService.java
@Path("/ws-api/availability/v1")
public interface GetAvailabilityService {
@GET
public Availability getAvailability(WebClient webClient,
@QueryParam("eventId") int eventId);
}
----
Class which i generated from xsd using JAXB
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "availability", propOrder = {
"event",
"groups"
})
@XmlRootElement
public class Availability {
protected SimpleDomainBean event;
protected Groups groups;
/**
* Gets the value of the event property.
*
* @return
* possible object is
* {@link SimpleDomainBean }
*
*/
public SimpleDomainBean getEvent() {
return event;
}
/**
* Sets the value of the event property.
*
* @param value
* allowed object is
* {@link SimpleDomainBean }
*
*/
public void setEvent(SimpleDomainBean value) {
this.event = value;
}
/**
* Gets the value of the groups property.
*
* @return
* possible object is
* {@link Groups }
*
*/
public Groups getGroups() {
return groups;
}
/**
* Sets the value of the groups property.
*
* @param value
* allowed object is
* {@link Groups }
*
*/
public void setGroups(Groups value) {
this.groups = value;
}
}
When i run this JUNIT test client, I am getting the below error:
INFO: Problem with setting the default provider
org.apache.cxf.jaxrs.provider.JSONProviderorg/codehaus/jettison/mapped/TypeConverter
Jun 4, 2012 5:50:23 PM org.apache.cxf.jaxrs.provider.ProviderFactory
createProvider
INFO: Problem with setting the default provider
org.apache.cxf.jaxrs.provider.JSONProviderorg/codehaus/jettison/mapped/TypeConverter
Jun 4, 2012 5:50:29 PM org.apache.cxf.jaxrs.client.AbstractClient
reportMessageHandlerProblem
SEVERE: .No message body reader has been found for class : class
com.tickets.ws.availability.Availability, ContentType :
text/html;charset=utf-8.
Error in
testGetAvailability:org.apache.cxf.jaxrs.client.ClientWebApplicationException:
.No message body reader has been found for class : class
com.tickets.ws.availability.Availability, ContentType :
text/html;charset=utf-8.
You may notice that I am alreading including the JacksonJsonProvider in the
WebClient.create method and also i have the @XmlRootElement in the
Availability.java.
Any thoughts on this will be helpful to me.
Thank You
Anil Mathew
--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-use-XSD-file-with-cxf-tp5708311p5709079.html
Sent from the cxf-user mailing list archive at Nabble.com.
RE: How to use XSD file with cxf
Posted by Abhishek Sharma <Ab...@makemytrip.com>.
That's done by the jaxb provider or gson parser present in cxf client. That will convert the xml/json to the Availability object.
I shared with you the standalone sample cxf client and that's working in my project.
Regards,
Abhishek
-----Original Message-----
From: amathewcxf [mailto:amathew@tickets.com]
Sent: Friday, June 01, 2012 10:42 AM
To: users@cxf.apache.org
Subject: RE: How to use XSD file with cxf
Thank you for the reply.
That's what i thought and i already generated the java objects from xsd
using the Eclipse IDE. Now your second point is where I have the issue.
i.e "Then you can use those objects in your cxf webservice client.". In my
example below,
Availability availability = getAvail.getAvailability();
Availability is one of the Java object which i generated via Eclipse based
on xsd.
What i don't understand is how will the cxf use the response data and gets
back the Availability java object.
Thanks
Anil Mathew
--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-use-XSD-file-with-cxf-tp5708311p5708960.html
Sent from the cxf-user mailing list archive at Nabble.com.
RE: How to use XSD file with cxf
Posted by amathewcxf <am...@tickets.com>.
Thank you for the reply.
That's what i thought and i already generated the java objects from xsd
using the Eclipse IDE. Now your second point is where I have the issue.
i.e "Then you can use those objects in your cxf webservice client.". In my
example below,
Availability availability = getAvail.getAvailability();
Availability is one of the Java object which i generated via Eclipse based
on xsd.
What i don't understand is how will the cxf use the response data and gets
back the Availability java object.
Thanks
Anil Mathew
--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-use-XSD-file-with-cxf-tp5708311p5708960.html
Sent from the cxf-user mailing list archive at Nabble.com.
RE: How to use XSD file with cxf
Posted by Abhishek Sharma <Ab...@makemytrip.com>.
No you need to generate your java objects from xsd using some maven plugin. Then you can use those objects in your cxf webservice client.
Maven plugin code pasted below for your reference:
<!--plugin to specify use java version 1.6 or above -->
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- Plugin to generate java objects from schema-->
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb22-plugin</artifactId>
<version>0.8.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<extension>true</extension>
<args>
<arg>-Xxew</arg>
<arg>-Xinheritance</arg>
<arg>-Xannotate</arg>
<arg>-Xcollection-setter-injector</arg>
<arg>-Xboolean-getter</arg>
</args>
<generatePackage>com. hotels.domain</generatePackage>
<generateDirectory>${project.build.sourceDirectory}</generateDirectory>
<plugins>
<!-- Plugin to remove redundant container classes for XMLElementWrapper-->
<plugin>
<groupId>xew</groupId>
<artifactId>xew</artifactId>
<version>1.0</version>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.3</version>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>0.6.3</version>
</plugin>
<plugin>
<groupId>net.java.dev.vcc.thirdparty</groupId>
<artifactId>collection-setter-injector</artifactId>
<version>0.5.0-1</version>
</plugin>
<plugin>
<groupId>com.nebulent.xjc</groupId>
<artifactId>boolean-getter</artifactId>
<version>0.5</version>
</plugin>
</plugins>
</configuration>
<dependencies>
<dependency>
<groupId>xew</groupId>
<artifactId>xew</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</plugin>
Regards,
Abhishek
-----Original Message-----
From: amathewcxf [mailto:amathew@tickets.com]
Sent: Friday, June 01, 2012 12:30 AM
To: users@cxf.apache.org
Subject: RE: How to use XSD file with cxf
Thanks you so much for your time and reply.
After reading your reply, i did lot of reading. The one question which i
have from your reply is, are you saying that I can refer the xsd in the
@Path like below?
@Path("/ws/GetAvailabilitySchema.xsd")
public interface GetAvailability{
@GET
public Availability getAvailability();
}
public class GetAvailabilityImpl implements GetAvailability{
@GET
public Availability getAvailability(){
}
}
Then I will have a standalone client code like below (which will be invoked
by a EJB method):
public class GetAvailabilityWSClient{
public void processWSClient(){
GetAvailability getAvail =
JAXRSClientFactory.create("http://remotewstest.com", GetAvailability.class);
Availability availability = getAvail.getAvailability();
}
}
Then the getAvail.getAvailability() in the GetAvailabilityWSClient will
return the Availability object based on the xsd?
Thanks Again
Anil Mathew
--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-use-XSD-file-with-cxf-tp5708311p5708925.html
Sent from the cxf-user mailing list archive at Nabble.com.
Re: How to use XSD file with cxf
Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
On 31/05/12 19:59, amathewcxf wrote:
> Thanks you so much for your time and reply.
>
> After reading your reply, i did lot of reading. The one question which i
> have from your reply is, are you saying that I can refer the xsd in the
> @Path like below?
>
>
> @Path("/ws/GetAvailabilitySchema.xsd")
You probably do not need to list the actual schema resource unless you
actually need to have a Path like that,
it served its purpose to get the model beans generated, and the code
below should work,
> public interface GetAvailability{
>
> @GET
> public Availability getAvailability();
>
> }
>
> public class GetAvailabilityImpl implements GetAvailability{
>
> @GET
> public Availability getAvailability(){
> }
>
> }
>
> Then I will have a standalone client code like below (which will be invoked
> by a EJB method):
>
> public class GetAvailabilityWSClient{
>
> public void processWSClient(){
> GetAvailability getAvail =
> JAXRSClientFactory.create("http://remotewstest.com", GetAvailability.class);
> Availability availability = getAvail.getAvailability();
> }
> }
>
> Then the getAvail.getAvailability() in the GetAvailabilityWSClient will
> return the Availability object based on the xsd?
>
After Availability has been generated, the schema can be used to
validate the payloads
Cheers, Sergey
>
> Thanks Again
> Anil Mathew
>
>
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/How-to-use-XSD-file-with-cxf-tp5708311p5708925.html
> Sent from the cxf-user mailing list archive at Nabble.com.
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com
RE: How to use XSD file with cxf
Posted by amathewcxf <am...@tickets.com>.
Thanks you so much for your time and reply.
After reading your reply, i did lot of reading. The one question which i
have from your reply is, are you saying that I can refer the xsd in the
@Path like below?
@Path("/ws/GetAvailabilitySchema.xsd")
public interface GetAvailability{
@GET
public Availability getAvailability();
}
public class GetAvailabilityImpl implements GetAvailability{
@GET
public Availability getAvailability(){
}
}
Then I will have a standalone client code like below (which will be invoked
by a EJB method):
public class GetAvailabilityWSClient{
public void processWSClient(){
GetAvailability getAvail =
JAXRSClientFactory.create("http://remotewstest.com", GetAvailability.class);
Availability availability = getAvail.getAvailability();
}
}
Then the getAvail.getAvailability() in the GetAvailabilityWSClient will
return the Availability object based on the xsd?
Thanks Again
Anil Mathew
--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-use-XSD-file-with-cxf-tp5708311p5708925.html
Sent from the cxf-user mailing list archive at Nabble.com.
RE: How to use XSD file with cxf
Posted by Abhishek Sharma <Ab...@makemytrip.com>.
Hi Anil,
For creating the webservice client all you need is an xsd and the URLs of the file.
You can configure your soa client either using jaxrs:client in spring bean or you can also write a standalone client.
I've used the standalone client and attached the same for your reference.
It's just an interface with the methods containing the URL(relative) as the @Path value.
The name of the method could be any but the URL and the signature must confirm the xsd definition and the RESTful soa url.
@POST
@Path("/search/v1.0/hotelSearch.xml")
SearchResponse search(SearchRequest searchRequest);
The searchRequest and SearchResponse are the objects I generated using an xsd only.
The class also contains a nested class CXFClient and the usage of the same will be like:
//Code actually calling the webservice:
String webServiceUrl = "http://servername/contextname";
SampleWSClient.CXFClient cxfClient = new SampleWSClient.CXFClient(webServiceUrl, "", "", 60000);
SearchResponse searchResponse = cxfClient.getService().search(searchRequest)
Regards,
Abhishek
-----Original Message-----
From: amathewcxf [mailto:amathew@tickets.com]
Sent: Wednesday, May 23, 2012 2:14 AM
To: users@cxf.apache.org
Subject: How to use XSD file with cxf
Hi,
I am a new user for cxf. We are trying to develop a webservice client
based on a xsd file. I was able to create domain classes from xsd file using
Eclipse. Now i wanted to call the remote API using this webservice client. I
am not sure how the cxf will be able to make use of my domain classes which
i generated from the XSD. The remote API is a REST-based application and
hence my request will have a querystring with the necessary data and the
response will be in JSON format.
In fact i read lot of documents, but i couldn't get a real picture. So
any thoughts will be very useful for me.
Thanks
Anil Mathew
--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-use-XSD-file-with-cxf-tp5708311.html
Sent from the cxf-user mailing list archive at Nabble.com.