Autogenerate java classes for facebook Graph Api service - java

Is there a way to have all the POJOs autogenerated that are needed when consuming the Faceboook Graph API Service via Jersey? Or is the only way to manually create all objects by looking at the graph API explorer output?
(The facebook.xsd does not work. It contains more classes than the API returns while not containing all of them (e.g. "post"). I don't think it is intended for the purpose of consuming the RESTful Graph service)
Thanks!

No, because RESTful APIs are not supposed to have a rigid, predefined structure.
But most java wrappers provide the most used classes like Post or User. Check restfb or spring-social-facebook.

Related

Creating Data Objects (or Entities) for a Restful Webservice in java

I am trying to create a RESTful webservice in java, but , as I am new to this, I am not sure
if there is a tool like wsimport (for SOAP based webservices) which can be used to create the Data objects or Entities (resources in REST world).
I searched the net for examples..But all of them seem to be the hello world types with no clear data modeling details.
How do I create the Data objects for a RESTful Webservice from scratch using just a XSD file ?
Any pointers will be helpful!
RESTful webservices do not have a contract like SOAP so automatic code generation for the services is difficult unless the RESTful webservices are using WADL (not very commonly used). If you are using WADL, you can use CXF to generate java code.
However if you have XSD files you can generate java code classes for them using JAXB (xjc is the command). This will work well for the Data model objects, service classes will probably need to be hand coded.
See the accepted answer in this question: How to generate JAXB classes from XSD?
A couple more links that might help:
JAXB XJC tutorial
If you use Intellij, see this

Automatically generate Retrofit Types

I'm a newie using Retrofit, and I was seraching for a automatic way to generate the class types for consume a rest web service with retrifit becouse as far as I've seen I'm suposed map all the objects that returns the server.
Specifically I want to work with a Endpoint Django REST framework 2.3.14 with a lot of elements by each method, Thats why I want to generate in an automatic way the element types.
I've seen some with JAX-RS but I'm not sure it works with django Rest Services.
Any help would be very appreciated.
Thanks
Use this [website]http://www.jsonschema2pojo.org, very useful to convert JSON to POJOs...

RESTful API with Spring MVC and GWT and overlay types

I have developed a simple Spring MVC RESTful API and now I moved to the stage to create a simple GWT project to perform some requests to this api and obviously I choose that the communication will be done by exchanging JSON messages.
When receiving a response I will have to unmarshall it to a POJO.
I am aware that the general approach is to create the so called 'overlay types' but that looks to me as a mere duplicate of the java classes I wrote in api.
So the question is:
why shouldn't I simply create a common api that simply contains the common classes to perform this marshalling/unmarshalling?
I can clearly see that the main benefit is that if any change is needed you won't have to change also the overlay types.
Assuming that you can define interfaces for your pojo, you can share those Interfaces in client and server side (common package)
In server side you have to code your implementations which are used for the RESTful api.
In client side, the implementation of those interfaces can be done automatically with generators. For this you can use gwtquery databinding or gwt autobeans.
To request your RESTful api, you can use either gwtquery ajax or gwt requestbuilder
Each option has its advantages, normally I use gwtquery because its simplicity and because its databinding approach is more lightweight, otherwise, with autobeans you can create your POJOS using autobeans factories in both client and server sides. If you already have developed your backend this is not a goal for you though.
The REST response can be consumed by any client and not specifically one client. If I understand your question correctly, you want to build the logic of marshalling and unmarshalling inside your REST API. Ideally it violates Single Responsibility Principal. You might need to change the mapping logic if the service changes so you are touching two different aspects of an API where as only one component requires change.
Also, the REST API should ideally be designed to be client agnostic. It is your specific requirement to translate them to POJO but another client might want to consume it as simple plain JSON. If you provide an overlay type, your code will be quite loosely coupled.
If your server side class (Player for example) can be serialized/desirialized without any problems, then you can send it to client side without any overlay type / conversion (serialization to JSON on server -> transport -> desirialization from JSON on client). On client side you can use RestyGWT for example to archieve automatic desirialization process. Overlay types and conversion process are necessary only in the case when Player instance cannot be serialized (for example it is backed by Hibernate).

Recommended Java packages/jars to access and process RESTful web services

I want to access an external RESTFul Web service via Java, and use an open source package that processes the returned XML or Json result and creates object(s) from this data.
I know that there are many solutions out there for this, and I'd like to get your feedback on which one I should use.
For accessing the web services, I know that I can use packages such as apache HttpClient etc. but I'm sure that there are packages that wrap this and also take care of processing the returned data (i.e. creating java objects from the result).
Thanks,
Nina
Spring is great, but this is one case where there are higher-level libraries out there that make it even easier. See for example the clients that come along with JAX-RS implementations like the Jersey client and the CXF client. Some implementations can even provide clients through dynamic proxying if you have a service interface and resource classes available. This way, you hardly have to write any code at all.
Spring Rest Template is your friend.
Spring MVC has something called "RestTemplate" which can be used exactly for this.
http://aruld.info/resttemplate-the-spring-way-of-accessing-restful-services/
http://blog.springsource.com/2009/03/27/rest-in-spring-3-resttemplate/

can java consume .NET object returned by ASP.NET web service?

i have an ASP.NET web service that returning a custom entity object (Staff):
[WebMethod]
public Staff GetStaffByLoginID(string loginID){}
how would i consume this in Java?
thanks!
ASP.NET automatically generates a WSDL that contains the interface definitions for your web methods and the types they consume/return.
Apache Axis provides a tool called WSDL2Java that will generated the all of the code you need to consume the webservice. Simply point it to:
http://yoursite.com/YourWebService.asmx?WSDL
If you browse directly to the .ASMX file, you'll get a nice test harness that you can use to explore the various methods you can call.
Once Axis reads your WSDL, it will generate some proxy classes, one of them will be based on the interface of Staff.
However, I would not use this class as your actual business object, and instead would wrap access to the web service through a service layer. This service layer would use the proxy Staff class to populate your real business object.
This protects your consuming code from any interface changes that may happen to the web service in the future, keeping the actual area of code that would be modified as small as possible.
I do this for a living, interopping between Java and .NET on many platforms using SOAP.
EDIT: Why the is this downvoted? It's the only correct answer here.
Just use Standard WSDL as mentioned by flyswat if you are using traditional asmx web services.
other solutions if not using standard ASP.NET Web Services:
Use REST
http://www.infoq.com/articles/REST-INTEROP
http://www.codeproject.com/KB/XML/WSfromJava.aspx
Make sure the objects are serializable and as long as the you can cast it to a similar class on the Java side, you are good. Else, you might have to write some custom class mappers in Java.
You may be able to do this by running Java on IKVM.

Categories

Resources