Hello CXF Gurus,
This is my first post and looking for a desperate solution. I have been
struggling on this issue from so many days by now. I have browsed through
various forums/blogs etc but unfortunately I could not make any those
solution work for me :-(
I have a CXF (I am using version 2.7.7) based Restful webservice up and
running in my local environment (OSGi container having embedded jetty in
it). I am trying to consume the RESTFul service through CXF based java
client (using Webclient). Though it is pretty straight forward as long as I
dont use namespaces witin my Request/Response beans. Moment I start using
the namespace (within my Request/Response Beans), I start getting the
dreaded JAXBException For example : unexpected element (uri:"",
local:"ns2.saveAlertsDSRequest"). Expected elements are
<{}saveAlertsRequest>.
There are 2 different ways (I am using them one at a time) I am specifying
the namespaces for my Request/Response beans.
(a) Using JaxB annotation
@XmlType(namespace="http://api.mycompany.com/dataservice/alerts")
(b) Using JaxB Annotation @XmlRootElement(name="saveAlertsRequest",
namespace="http://api.mycompany.com/dataservice/alerts")
Here is one example :
package com.company.api.dataservice.alert.vo;
@XmlType(namespace="http://api.company.com/dataservice/alerts")
@XmlRootElement(name="saveAlertsDSRequest")
@XmlAccessorType(XmlAccessType.FIELD)
public class SaveAlertDSRequest{
private String id;
private String alert;
// public Setters/getters....
}
When I provide namespaces using @XmlType, I can easily hit the service from
Firefox RestClient and get the proper response but I CAN NOT access it
through Java client (using CXF Webclient). It gives the same Jaxb namespace
problem.
When I provide namespaces using @XmlRootElement, I can access the service
through Java client (e.g. CXF Webclient) without any issues, but I cant
access it (gives the same Jaxb namespace problem) from Firefox RestClient
I am creating the stubs (using ObjectFactory, Request/Response POJOs) using
cxf-wadl2java-plugin to create java client to hit the RESTful service.
Below is the code snnipet which I am working with :
=============================================================
public static void main(String args[]){
String path = "http://localhost:7070/api/alert/preferences/v1";
//List providers = new ArrayList();
//providers.add(new JacksonJsonProvider());
MetadataMap<String, String> headers = new MetadataMap<String,
String>();
headers.putSingle("Content-Type", "application/json");
WebClient webClient = null;
try {
webClient = setupMyWebClient(path,
headers,MediaType.APPLICATION_XML);
//webClient.query("environment", "dev");
//webClient.put(body, responseType)
webClient.accept("application/json");
ObjectFactory objFacory = new ObjectFactory();
SaveAlertsDSRequest saveAlertsDSRequest =
objFacory.createSaveAlertsDSRequest();
saveAlertPreferencesDSRequest.setId("1");
saveAlertPreferencesDSRequest.setAlert("my test alert");
JAXBElement<SaveAlertPreferencesDSRequest>
jaxBSaveAlertPrefDSRequest =
objFacory.createSaveAlertPreferencesDSRequest(saveAlertPreferencesDSRequest);
//JAXBElement<SaveAlertsDSRequest>
jaxBSaveAlertsDSRequest = new
JAXBElement<SaveAlertsDSRequest>(new QName("uri","local"));
//JAXBElement<SaveAlertsDSRequest>
jaxBSaveAlertsDSRequest = new
JAXBElement(new javax.xml.namespace.QName("",
"{}"),javax.xml.bind.JAXBElement.class, null,saveAlertPreferencesDSRequest
);
com.mycompany.api.dapi.a.SaveAlertPreferencesDSResponse
response =
webClient.put(jaxBSaveAlertPrefDSRequest,com.mycompany.api.SaveAlertsDSResponse.class
);
System.out.println(">>>>>>>>>>>>
"+response.getResponse());
//***********************************************************************************************************************
/* ObjectFactory objFacory = new ObjectFactory();
JAXBElementProvider<?> provider = new JAXBElementProvider();
provider.setJaxbElementClassMap(Collections.singletonMap("com.mycompany.api.alerts.SaveAlertsDSRequest",
"{http://api.mycompany.com/dapi/alerts}saveAlertsDSRequest"));
SaveAlertPreferencesDSResponse bookStore =
JAXRSClientFactory.create("http://localhost:7070/api/alert/preferences/v1",
SaveAlertPreferencesDSResponse.class,Collections.singletonList(provider));
SaveAlertsDSRequest saveAlertsDSRequest =
objFacory.createSaveAlertsDSRequest();
saveAlertPreferencesDSRequest.setId("1");
saveAlertPreferencesDSRequest.setAlert("my test alert");
Response rrr =
WebClient.client(saveAlertsDSRequest).getResponse();
System.out.println(">>>>>>>>>>>>. "+rrr.getStatus());*/
//***********************************************************************************************************************
} catch (ConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static WebClient setupMyWebClient(String path,
MetadataMap<String,
String> httpHeaders, String mediaType) throws ConfigurationException{
// To do, do we need it to read from properties or from Mindi
//String baseAddress = getConfigBean().getBaseAddress();
//String baseAddress =
"http://localhost:7070/api/alert/preferences/v1";
// Set up the WebClient
WebClient webClient = WebClient.create(path,
addJsonProviders1());
//WebClient webClient = WebClient.create(path);
//webClient.path(path);
webClient.accept(mediaType);
webClient.headers(httpHeaders);
// Add any secure connection details if any.
return webClient;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private static List addJsonProviders1()
{
List providers = new ArrayList();
ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector introspector = new
JaxbAnnotationIntrospector();
mapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE,
true);
mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
mapper.getDeserializationConfig();
mapper.setAnnotationIntrospector(introspector);
//providers.add(new JacksonJaxbJsonProvider(mapper,
JacksonJaxbJsonProvider.DEFAULT_ANNOTATIONS));
providers.add(new JSONProvider());
return providers;
}
===============================================================
I have also tried the recommendations suggested here, but it didn’t work at
all for me.
I would really appreciate if someone can help.
Thanks,
Jameel
--
View this message in context:
http://cxf.547215.n5.nabble.com/CXF-JAX-RS-javax-xml-bind-UnmarshalException-unexpected-element-uri-local-ns2-Expected-elements-are-tp5740027.html
Sent from the cxf-user mailing list archive at Nabble.com.