Java client & server app: Serialization or REST / SOAP / RPC? - java

I am writing application in Java. It has 2 parts, server and client.
I need to communicate between server and client (secure using SSL). I don't have any other non-Java app connected to server (in long future too not), so I am not dependent on someone else's API.
Most of the data will be strings, numbers, and other "common" data types.
Is it good idea to use Java Serialization to communication between server and client? Or "good way" is use SOAP / XML / RPC / REST / ... ?
I found "Hessian binary web service protocol" is it good or is it waste of time?
Thank you for help. This is my first bigger client-server app.

Why do you want to restrict your service to java applications? It wouldn't be easier to develop and in the future you possible have to change it to other applications be able to communicate with.
I would suggest you to build a Rest Api serializing your data with JSON. It is simple and you can find lot of information about it, and libraries which do everything automatically, almost. To serialize the information you can use libraries like Jackson and to build your Rest Api Server you can use Spring MVC, you have a simple example here.
For the client you can use Jax-Rs 2.0 .
I hope it has been helpful! Ask anything!

Related

Alternatives to RMI

I have a small Java SE application, it´s practically a fat client sitting on top of a database. To further my Java skills, I decided to make a client-server application out of it. The server application communicates with the database and handles all kinds of lengthy operations, while the client application only receives the results, mostly ArrayLists of moderate length and primitives.
To this end, I started reading on RMI and did the Oracle tutorial, which I found surprisingly difficult to understand and even get to work.
Is there anything else I can use instead of RMI, without having to dive into JavaEE?
One way I would suggest would be to use JSON as the format for data exchange. You can use GSON to convert the data from Java objects to JSON and back. The transport could be done directly on the HTTP protocol using REST. You can use Jersey as a REST server/client or roll your own (since you don't want to use JEE, which Jersey is part of).
SIMON is as simple to use as RMI, but with fewer traps in the initial setup. It also has some advantages over RMI. Here is a simple hello-world example:
http://dev.root1.de/projects/simon/wiki/Sample_helloworld110
I take it RMI = Remote Method Invocation ...
You can have a look at XMLRPC, JSONRPC, JMS, or if you want to roll your own, use JSON to POST messages and convert the JSON back to a java object on the other side using GSON (from Google) or setup RabbitMQ and use AMQP to submit and receive messages if you don't want to handle the POSTing yourself, Spring AMQP makes it fairly easy to do this.

Building java server which share interfaces wtih Android client

2 month ago i started to develop an android application which needs to call remote methods and receive complex objects (custom objects with custom feilds in it) from a server.
My friend and I splitted the work so he worked on the android client and i on the server.
Before we started, we built the base interfaces which provide the functions that the client needs from the server, so my friend can program easly the application (by using fake classes as implementation for the interfaces), and after i finish the implemntations of the interfaces in the server-side he will make the connection and call the functions from the server and not from the fake classes.
Now the problem is that we can't find a way to pass those interfaces from the server to the client.
We tried to use java RMI, but we faild because android doesn't support java RMI,
then we tried to use JAX-WS (with tomcat 7) and we also faild because JAXB can't handle intefaces. (-you can see more details here about jaxb issue-)
My friend and I feel really lost.. we don't have any idea how to pass those interfaces between the server and the android client.
Is it possible what we're trying to do? if not,
what other options avaible for us to call remote methods and receive complex objects from the server?
Thanks!
You can expose webservices on the Server, so the client can interact with the server whenever its needed that might be quickest solution.
Or you can write a kind of servlet programming to get the json request from the client, process it and send the json respoonse back to the client. If the application is data intensive, the JSON helps you a lot
Not sure if this is too late now (after 2 months of development), but there are frameworks that should make RPC easier for you (take care of linking both ends). Two I know of are Apache Thrift (definitely usable with Android - there are apps that use it) or Apache Etch (possibly).
Apache Thrift:
http://thrift.apache.org/
Apache Etch:
http://incubator.apache.org/etch/
Blog about Evernote choice of Thrift:
http://blog.evernote.com/tech/2011/05/26/evernote-and-thrift/
If your application is limited to communication between Java on the server and Android (no other clients e.g. IOS) then an easier RPC path compared with IDL based solutions is to use jsonrpc. This solution provides both server and Android client components. It is extremely easy to implement on both client and server. One limitation is that byte arrays have to be encoded because the JSON transport does not support binary.

Simple web services in Java applications

I am new to web service and WSDL. I have a MySQL database stored in a server and I need to create a very simple web service that allows third party Java applications to input an integer (say 'membership number') and receive a string (say 'member name'). This should be done with WSDL.
Can someone please tell me what files do I need? A WSDL file, a schema file? Do I need a Java class stored in the server responsible to communicate with the database?
I am pretty lost, I would be thankful if you could give me some suggestion
As the simplest form of webservice is you can continue with JAX-WS, upon deployment of appilication it will publish a WSDL file and you can use this WSDL to communicate it from client
JAX-WS is the simplest way to convert an existing method into a web service endpoint, with a few annotations, and the simple web server in the standard Java 6 JRE. It can autogenerate the WSDL too.
This tutorial covers the essentials for getting started: http://java.dzone.com/articles/jax-ws-hello-world
I have found this to be a very robust technology.
Using the SOAP Webservices can get pretty messy. Unless you are forced, I would recommend you use REST for webservices, it is much less complex than SOAP. You can find a good REST tutorial here.
If on the other hand you must use SOAP, you could strat by taking a look here.

Android JSON vs REST

im starting with web services. I'm working in a project that needs to communicate Android with a web server and I'd like to save some time chosing the appropriate protocol for the communication.
Between JSON ,REST and SOAP:
Which ones can I run in a non-dedicated server?
Whats the best choice for a high trafic server?
Thanks in advance
JSON and REST are not mututally exclusive. JSON is a data format that the REST interface can return.
You may run either on a non dedicated server. I would personally choose REST for rapid prototyping on Android as it is easier to get up and running. (With SOAP you will probably want to have a schema which takes time to put together)
There's a good comparison between REST and SOAP on the REST wiki article: http://en.wikipedia.org/wiki/Representational_State_Transfer#Concept
REST is more of a framework than a communications protocol. JSON and SOAP could be use to create a RESTful application.
If you're writing something with many resources, I'd use REST as it is far more structured. There are also a lot of libraries that will set most of things up for you. I find JSON and SOAP are better for custom functionality that you want to hack together quickly. They can be lightweight, but less structured.

best way to communicate between .net and java

I have a .net application which needs to expose a service consumed by a java client. The service can't be public. There should be some authentication mechanism for the client. What is the best way to do this? I'm new to web services and am confused by all the soap, wsdl etc. and have also heard a lot that it'll be a pain to get the two to communicate. Your thoughts?
Web Services are the one of the best approaches for interoperability over WEB. it's not that much difficult to create and use. It's as simple as Connecting to different databases and writing XML parsing in your code.
There are so many ways doing authentication. If you are using the IIS for deploying .net web services, then You can use Windows Authentication, Forms Authentication. These 2 are out of the box features of IIS. You can implement your custom authentication also. There are few compatible cryptography algorithms also for encrypting your data.
Without web services, it's very difficult to establish communication between Java ad .net.
You can give a try for JSON also. But i am not sure.
Hey check this video which gives you details. http://skillsmatter.com/podcast/java-jee/consuming-a-dot-net-web-service-using-java
I'd go for a simple REST interface using something like Jersey on the Java side, and a .Net JSON library to parse the data I consume.

Categories