You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by fahman_dude <fa...@hotmail.com> on 2010/02/18 15:13:35 UTC

RESTful services without annotations

Hello,

I am trying at the moment to implement a RESTful Service without annotations
(as in using "user model" instead) and using this (for my taste bit
confusing) guide: http://cxf.apache.org/docs/jax-rs.html

Since my implementation does not work I am trying to understand the whole
mechanics in-depth and am stuck at the moment on this example:
http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerUserResourceTest.java

The question I have here, is: Why is it required to programmatically create
UserRessource instances

<snip>
JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();

...
UserResource ur = new UserResource();
ur.setName(BookStoreNoAnnotations.class.getName());
ur.setPath("/bookstoreNoAnnotations");
...
 sf.setModelBeans(ur);
...
</snap>

in addition to setting ModelRef?

<snip>
...
String modelRef =
"classpath:/org/apache/cxf/systest/jaxrs/resources/resources2.xml";
sf.setModelRefWithServiceClass(modelRef,
BookStoreNoAnnotationsInterface.class);
...
</snap>

Isn't it so that the model already describes EVERYTHING UserResource
instances do. When I don't create UserResource instances I get this error:

ERROR [main] apache.cxf.jaxrs.AvstractJAXRSFactoryBean (231) - No resource
classes found

My code looks like this:

...
JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
sf.setAddress("hhtp:/...");
sf.setModelRef("classpath:/to/my/modelref.xml");
sf.setServiceBeans(new MyWSImplementationNoAnnotations);
sf.create()
...

modelref.xml looks like this:

<model xlmns="http://cxf.apache.org/jaxrs">
	<resource name="MyWSImplementationNoAnnotationsInterface"
path="mywsservice"
		produces="application/json" consumes="multipart/form-data">
		<operation name="do" verb="POST" path="/do">
			
			
		</operation>
	</resource>
</model>

And the

public class MyWSImplementationNoAnnotations implements
MyWSImplementationNoAnnotationsInterface {
...
public void do(ComplexClass docontext, AnotherComplexClass domoreStuff) {
...
}
}

cheers
reinis
-- 
View this message in context: http://old.nabble.com/RESTful-services-without-annotations-tp27637811p27637811.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: RESTful services without annotations

Posted by fahman_dude <fa...@hotmail.com>.
Hello,

I fixed the error... by creating a new xml-file in eclipse. Instead of
copying model definition out of the guide and then adjusting, I created xml
out of the jaxrs.xsd referenced in the guide. After I did that, everything
started to work.

I assume there were some invisible chars or something that was just too much
for the DOM to handle.

For everyone else I would suggest to use this schema aswell (currently:
http://svn.apache.org/repos/asf/cxf/trunk/rt/frontend/jaxrs/src/main/resources/schemas/jaxrs.xsd).

cheers
reinis
-- 
View this message in context: http://old.nabble.com/RESTful-services-without-annotations-tp27637811p27676890.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: RESTful services without annotations

Posted by fahman_dude <fa...@hotmail.com>.
Hello,

I was browsing through the logs the other day and noticed this:
WARN  [main] cxf.jaxrs.utils.ResourceUtils (373)     - Problem with
processing a user model at
classpath:/org/apache/cxf/systest/jaxrs/resources/resources2.xml

First, I assumed that I wasted your time since the error is that the
resources2.xml simply cannot be found on the classpath... but unfortunatelly
it could.

So I finally downloaded cxf sources and dug down along the stack.

This is what I found inside (cxf-common-utilities-2.2.6.jar,
cxf-rt-frontend-jaxrs-2.2.6.jar):

java.lang.NullPointerException
	at java.lang.String.contains(String.java:2103)
	at
org.apache.cxf.helpers.DOMUtils.findAllElementsByTagNameNS(DOMUtils.java:603)
	at
org.apache.cxf.helpers.DOMUtils.findAllElementsByTagNameNS(DOMUtils.java:608)
	at
org.apache.cxf.helpers.DOMUtils.findAllElementsByTagNameNS(DOMUtils.java:596)
	at
org.apache.cxf.jaxrs.utils.ResourceUtils.getResourcesFromElement(ResourceUtils.java:401)
	at
org.apache.cxf.jaxrs.utils.ResourceUtils.getUserResources(ResourceUtils.java:396)
	at
org.apache.cxf.jaxrs.utils.ResourceUtils.getUserResources(ResourceUtils.java:371)
	at
org.apache.cxf.jaxrs.AbstractJAXRSFactoryBean.setModelRefWithServiceClass(AbstractJAXRSFactoryBean.java:289)

The statement:

if (localName.equals(el.getLocalName()) &&
nameSpaceURI.contains(el.getNamespaceURI())) {

in
org.apache.cxf.helpers.DOMUtils.findAllElementsByTagNameNS(DOMUtils.java:603)
is causing this NullPointerException in the
nameSpaceURI.contains(el.getNamespaceURI()) because NamespaceURI of the el
(in my case that would be "resource" element from user model xml) is null.

I am sorry to bother you guys, but since last time I worked with DOM
directlly was like 8 years ago, am quite puzzled about this error. I
explicitelly set xlmns="http://cxf.apache.org/jaxrs" attribute of 'model'
element. Can someone point me to the reason why the namespace uri in the
parsed DOM Document instance is null?

cheers
reinis
-- 
View this message in context: http://old.nabble.com/RESTful-services-without-annotations-tp27637811p27675688.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: RESTful services without annotations

Posted by Sergey Beryozkin <sb...@progress.com>.
> S.B : note that in 2. the test uses sf.setModelRefWithServiceClass(modelref, SomeClass.class) but it could've just used
> sf.setModelRef(modelref) even with this model describing an interface only. sf.setModelRefWithServiceClass() is used mainly in
> (D)OSGI where the OSGI runtime has already loaded an interface class.

and

> S.B : If you have a model describing an interface only then you need to call setModelRefWithServiceClass as it is done in the test

there's a contradiction here : one have to use setModelRefWithServiceClass only in the (D)OSGI case, or when you have the interface 
class already loaded. Otherwise setModelRef(modelref) will do

cheers, Sergey 


Re: RESTful services without annotations

Posted by Sergey Beryozkin <sb...@progress.com>.
Hi

Please see comments inline.

thanks, Sergey

----- Original Message ----- 
From: "fahman_dude" <fa...@hotmail.com>
To: <us...@cxf.apache.org>
Sent: Thursday, February 18, 2010 2:13 PM
Subject: RESTful services without annotations



Hello,

I am trying at the moment to implement a RESTful Service without annotations
(as in using "user model" instead) and using this (for my taste bit
confusing) guide: http://cxf.apache.org/docs/jax-rs.html

> S.B : recommendations are welcome. David K. has helped quite a bit - let me know if you'd like certain things be better expanded 
> upon, etc...

Since my implementation does not work I am trying to understand the whole
mechanics in-depth and am stuck at the moment on this example:
http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerUserResourceTest.java

The question I have here, is: Why is it required to programmatically create
UserRessource instances

> S.B there's no such requirement. This test just shows that one can progarammatically register a user model reference.

<snip>
JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();

...
UserResource ur = new UserResource();
ur.setName(BookStoreNoAnnotations.class.getName());
ur.setPath("/bookstoreNoAnnotations");
...
 sf.setModelBeans(ur);
...
</snap>

in addition to setting ModelRef?

<snip>
...
String modelRef =
"classpath:/org/apache/cxf/systest/jaxrs/resources/resources2.xml";
sf.setModelRefWithServiceClass(modelRef,
BookStoreNoAnnotationsInterface.class);
...
</snap>

> S.B :

In this particular code snippet two things are happening :

1. User model is created programmatically and the resource class which is beng described is BookStoreNoAnnotations.class which is a 
concrete class

2. Another user model (resource2.xml) is being linked to, this model describes an interface only (BookStoreNoAnnotationsInterface), 
hence we also register a concrete class implementing it (BookStoreNoAnnotationsImpl). This latter option is partucularly effective 
in the OSGI case or when you have a lot of dynamic resources coming in...It also works well on the client proxy side where there's 
no need to register an actual implementation.

> S.B : note that in 2. the test uses sf.setModelRefWithServiceClass(modelref, SomeClass.class) but it could've just used 
> sf.setModelRef(modelref) even with this model describing an interface only. sf.setModelRefWithServiceClass() is used mainly in 
> (D)OSGI where the OSGI runtime has already loaded an interface class.

Isn't it so that the model already describes EVERYTHING UserResource
instances do. When I don't create UserResource instances I get this error:

> S.B : do you have an actual requirement to create them programmatically, it should all be hidden if you link to a model from 
> Spring...

ERROR [main] apache.cxf.jaxrs.AvstractJAXRSFactoryBean (231) - No resource
classes found

My code looks like this:

...
JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
sf.setAddress("hhtp:/...");
sf.setModelRef("classpath:/to/my/modelref.xml");
sf.setServiceBeans(new MyWSImplementationNoAnnotations);
sf.create()
...

> S.B : If you have a model describing an interface only then you need to call setModelRefWithServiceClass as it is done in the test

modelref.xml looks like this:

<model xlmns="http://cxf.apache.org/jaxrs">
<resource name="MyWSImplementationNoAnnotationsInterface"
path="mywsservice"
produces="application/json" consumes="multipart/form-data">
<operation name="do" verb="POST" path="/do">


</operation>
</resource>
</model>

And the

public class MyWSImplementationNoAnnotations implements
MyWSImplementationNoAnnotationsInterface {
...
public void do(ComplexClass docontext, AnotherComplexClass domoreStuff) {
...
}
}

> S.B : All seems fine. Can you please post me the model.ref with these two sample classes ? Actually your model does not descrive 
> the actual parameters docontext and domoreStuff.  It's a multipart/form-data that is expected, these parameters, do they have to 
> be mapped to indiv attachment parts ? If so they you'd probably need to mark both in the model as parameters of REQUEST_BODY...

let me know how it goes please, Sergey

cheers
reinis
-- 
View this message in context: http://old.nabble.com/RESTful-services-without-annotations-tp27637811p27637811.html
Sent from the cxf-user mailing list archive at Nabble.com.