jax rs 2.0 annotation inheritance not working - web-services

I'm reading Bill Burke's Rest services with Jax rs 2.0 book. However, I'm stuck at this point.
If I create a CustomerService as a concrete class with annotations, my IDE is picking it up as a RestWebService and deploys it.
#Path("/customers")
public class CustomerService {
....
#POST
#Consumes("application/xml")
public Response createCustomer(InputStream is)
....
}
But if use an interface and annotate it, and implement it in a class (the concrete class doesnt have annotations), the ide doesnt recognise it. I get HTTP 404 error status.
I'm using Netbeans 7.4 and JBoss AS 6.2.
Please help! Thanks.

From specification:
A resource class is a Java class that uses JAX-RS annotations to implement a corresponding Web resource.
Resource classes are POJOs that have at least one method annotated with #Path or a request method designator.
You can't load classes without annotations as resources.

Related

CakePHP model to web service

I'm developing a web application that doesn't need database. All data comes via REST.
My question is: Is possible to use a model class (extended from AppModel) to manage data from a RESTfull service?, How to do that?, Does CakePHP any way to mapping directly a RESTfull web service?.
I have researched and I have found the $useTable attribute (http://book.cakephp.org/2.0/en/models/model-attributes.html#usetable) but I'm not sure if exists another way to do this in a better way.
My first idea is implement my own functions in a Model and put $useTable to false.
Thank you.
There is no such functionality that ships with CakePHP, but it's relatively easy to implement it on your own.
Check out datasources, they sit between the model and the data, but can also be used standalone, ie you can use datasource instances directly, or indirectly through the model layer (find/save/delete calls are using the datasource CRUD methods).
Checkout https://github.com/nodesagency/CakePHP-Rest-Datasource for some inspiration.

Correct way of initializing internal data for API in Django with Piston/Django-tastypie vs jsonrpc

After some API implementations, using jsonrpclib in Python, I need to migrate them inside a Django Framework project. I am quite new in Django and Piston/tastypie, but have some experience using jsonrpc/xmlrpc libs in my Python apps.
Until now I have developed some modules, with a ServiceClass attached to the register of jsonrpc server who handle the request and call the methods in the ServiceClass.
When the class is attached to the register, a new instance of the ServiceClass is created, loading all the initial data and keeping it in memory, so every method called through jsonrpc can have access to the internal values in that instance.
Now, I am trying to do the same in Django with Piston or Tastypie. I followed this link http://www.robertshady.com/content/creating-very-basic-api-using-python-django-and-piston and other resources, and all the documentation I read is clear, showing the correct way to work with it:
Modify url.py to map requests like "/api/" to a specific handler.
Add a handler.py in the api application, extending the BaseHandler of
Piston/Tastypie.
So I am wondering if its the correct way of working with Django and APIs, to create the instance of my ServiceClass (init the data, provide the methods) inside handler.py when I create the instance of the Handler extending the BaseHandler. Is this Handler class instantiated once when the server starts? What if my ServiceClass relies on some Model to load the data from it?
I want to avoid the framework to instantiate my class everytime a new request arrive to the /api/ application.
I will be glad to hear about any recommendation,
Thanks,
Specifically for piston... You shouldnt really use the handler in terms of an instance. Its more like a metaclass that you set up with class attributes. These attributes control whicch model the handler will be bound to if any. And what fields it should show or what methods it supports.
Generally the request enter one of your methods and you then handle the request however you want, as an isolated state. If it needs to use a shared resource or use the model for queries, that part is up to you, being shared from some imported resource . You said you need a model which is why you would bind it to the handler as a class attribute and then query on it. You shouldnt really store state on the handler.

How to call a webservice programmatically in asp.net

How to call webservice programmatically in asp.net without using add web reference?
My webservice url keeps on changing. Hence i need to capture the url at runtime and display the results.
Please advice.
You can change the URL of a web-reference at runtime (provided that the new address is hosting a service with the same schema that you originally used to create the reference):
MyWebService ms = new MyWebService();
ms.Url = "http://example.com/webservice.asmx";
ms.MyWebMethod();
Web References are definitely the way to go - whilst the classes that are created by the web reference are usually pretty heavy, all that strong typing makes it well worth your while.
you need to do the following steps.
PreReq :
First of all, you know the URL of web service.
Solution:
use wsdl.exe to create a proxy class and than compile it.
wsdl /out:myProxyClass.cs http://hostServer/WebserviceRoot/WebServiceName.asmx?WSDL
(there are other switches available
for wsdl. For Example to generate VB
class, you need to add switch
/language:VB)
Once your proxy class is generated you can easily consume in code.
MyProxyClass objService = new MyProxyClass();
DateTime time = objService.GetServerTime(); //Suppose service has method getServerTime
You can specify the end-point URL as part of the constructor of your client-side proxy class.
If you don't need to specify it during runtime then it can also be set in your web.config file.
Where are you trying to call the service and where the service file is located?
If the service is located on the same site. Why not just instantiate the class name from the service. Or just create a separate class and use interface

Should I be using the xfire plugin with Gralis or is there a better way?

I have a web service in Grails, published using the xfire plugin. It's all fine and can be connected to no problem at all. But I'd like to modify the format of the request and response. Is that possible?
The example structure is:
Domain class Person, has many Siblings. Each Sibling has a Child.
My web serice method currently looks something like this:
Person updatePerson(Person person) {
return Person.getById(person.id)
}
But that produces a WSDL with the format:
Person
Person fields
Sibling
Sibling fields
Child
Sibling
Sibling fields
etc etc.
I don't want to accept the data in this format, or return it in that format. Is there a way of specifying which fields are exposed to the web service and what the WSDL should look like, or is xfire the wrong plugin to use for this?
I would answer this as its creator.
To exclude some domain properties, add:
static xmlTransients = ['your_property_goes_here']
to your domain classes.
However, the plugin does not allow you to control creation of the WSDL directly.
I would recommend looking at the Spring WS plugin. It's designed around "contract-first" web service development, so you have complete control over the resulting WSDL. The Xfire, Axis and Metro plugins are all great, but from my experience are all geared towards code-first and don't allow direct control over the resulting interface/output.

What is tempuri.org?

Why does tempuri.org exist? Why does each XML Webservice require its own namespace, unique from any other on the web?
Unfortunately the tempuri.org URL now just redirects to Bing.
You can see what it used to render via archive.org:
https://web.archive.org/web/20090304024056/http://tempuri.org/
To quote:
Each XML Web Service needs a unique namespace in order for client
applications to distinguish it from other services on the Web. By
default, ASP.Net Web Services use http://tempuri.org/ for this
purpose. While this suitable for XML Web Services under development,
published services should use a unique, permanent namespace.
Your XML Web Service should be identified by a namespace that you
control. For example, you can use your company's Internet domain name
as part of the namespace. Although many namespaces look like URLs,
they need not point to actual resources on the Web.
For XML Web Services creating[sic] using ASP.NET, the default namespace can
be changed using the WebService attribute's Namespace property. The
WebService attribute is applied to the class that contains the XML Web
Service methods. Below is a code example that sets the namespace to
"http://microsoft.com/webservices/":
C#
[WebService(Namespace="http://microsoft.com/webservices/")]
public class MyWebService {
// implementation
}
Visual Basic.NET
<WebService(Namespace:="http://microsoft.com/webservices/")> Public Class MyWebService
' implementation
End Class
Visual J#.NET
/**#attribute WebService(Namespace="http://microsoft.com/webservices/")*/
public class MyWebService {
// implementation
}
It's also worth reading section 'A 1.3 Generating URIs' at:
http://www.w3.org/TR/wsdl#_Toc492291092
Webservices require unique namespaces so they don't confuse each others schemas and whatever with each other. A URL (domain, subdomain, subsubdomain, etc) is a clever identifier as it's "guaranteed" to be unique, and in most circumstances you've already got one.
http://en.wikipedia.org/wiki/Tempuri
tempuri.org is the default namespace URI used by Microsoft development products, like Visual Studio.
Probably to guarantee that public webservices will be unique.
It always makes me think of delicious deep fried treats...
Note that namespaces that are in the format of a valid Web URL don't necessarily need to be dereferenced i.e. you don't need to serve actual content at that URL. All that matters is that the namespace is globally unique.