Hi
The appears to be some issue with your invocation handler, why do you
need it all ? If users had to write InvocationHandlers in order to be
able to work with CXF RS client runtime then it would not be ideal at
all :-)
Thanks, Sergey
On 20/02/13 16:49, allam-di wrote:
Hello all,
I would like to use MOXY in order to handle interfaces as input arguments on
my service methods.
First, I tested MOXY data binding with interfaces as it is given in the
following blog:
http://blog.bdoughan.com/2010/07/moxy-jaxb-map-interfaces-to-xml.html
It works oK for me, thus I can serialize and deserialize messages by
declaring a service method with an interface argument, without defining any
implementation classes of this interface.
In order to do a similar think integrated to cxf, I followed the discussion
at this link:
http://cxf.547215.n5.nabble.com/How-to-configure-CXF-to-use-different-JAXBContextFactory-td5281773.html
Here is my ContextResolver Class:
-----------------------------------------
package project;
@Provider
public class MyContextResolver implements ContextResolver<JAXBContext> {
private JAXBContext jc;
public MyContextResolver() {
try {
jc = JAXBContext.newInstance(MyObjectFactory.class);
} catch(JAXBException e) {
throw new RuntimeException(e);
}
}
public JAXBContext getContext(Class<?> type) {
try {
jc = JAXBContextFactory.createContext(new Class[]
{MyObjectFactory.class}, null);
return jc;
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}
}
Here is my ObjectFactory class:
-----------------------------------------
@XmlRegistry
public class MyObjectFactory {
public Customer createCustomer() {
return createInstance(Customer.class);
}
...
private<T> T createInstance(Class<T> anInterface) {
return (T) Proxy.newProxyInstance(anInterface.getClassLoader(), new
Class[] {anInterface}, new InterfaceInvocationHandler());
}
private static class InterfaceInvocationHandler implements
InvocationHandler {
private Map<String, Object> values = new HashMap<String, Object>();
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
String methodName = method.getName();
if(methodName.startsWith("get")) {
return values.get(methodName.substring(3));
}
else {
values.put(methodName.substring(3), args[0]);
return null;
}
}
}
}
Here is my bean.xml file:
-----------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxrs:server id="test" address="/">
<jaxrs:serviceBeans>
<ref bean="myWebService" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="MOXYProvider" />
</jaxrs:providers>
</jaxrs:server>
<bean id="MOXYProvider" class="project.MyContextResolver" />
<bean id="myWebService" class="project.Service" />
</beans>
Here is a the constructor of my service class:
----------------------------------------------------
public Service(){
ObjectFactory objectFactory = new ObjectFactory();
Customer c = objectFactory.createCustomer();
c.setName("defaultName");
Address address = objectFactory.createAddress();
address.setCity("defaultAddress");
address.setStreet("defaultStreet");
c.setAddress(address);
this.resources.put(c.getName(), s);
}
and here is my GET method:
@GET
@Path("/resources/{name}/")
public Customer getCustomer(@PathParam("name") String name) {
return this.resources.get(name);
}
When I call my service, I get the following error:
Exception in thread "main" Status : 500
Headers :
Date : Wed, 20 Feb 2013 16:02:30 GMT
Content-Length : 65
Content-Type : text/plain
Connection : close
Server : Apache-Coyote/1.1
Error message :
No message body writer has been found for response class $Proxy5.
at
org.apache.cxf.jaxrs.client.ClientProxyImpl.checkResponse(ClientProxyImpl.java:250)
at
org.apache.cxf.jaxrs.client.ClientProxyImpl.handleResponse(ClientProxyImpl.java:517)
at
org.apache.cxf.jaxrs.client.ClientProxyImpl.doChainedInvocation(ClientProxyImpl.java:487)
at
org.apache.cxf.jaxrs.client.ClientProxyImpl.invoke(ClientProxyImpl.java:188)
at $Proxy18.getResource(Unknown Source)
at project.Client.main(Client.java:35)
Any idea about this error please?
Regards,
Diana
--
View this message in context:
http://cxf.547215.n5.nabble.com/How-to-configure-cxf-to-use-MOXY-Jaxb-for-RESTful-tp5723506.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