Passing json data between client and server - java

I need to implement web services, send json to a server and read the response. All of the requests are send to the service https://api.polldaddy.com/
This will take place within a web application. This is the api I have to implement :
http://support.polldaddy.com/api/
It seems straightforward, just send some json to the server and consume the json response that is sent back. There seems to be so many options of doing this task that its a little daunting where to start ?
So, where is the best place to start in learning how to implement this service, ie : send json to a server and consume the response.

First of all, you are using the wrong terminology. "Implement web services" implies you will create a service, it sound like you just want to call a web service. You could say "leverage web services" if you need it to be business speak complaint.
The harder way. If you can't add on any additional libraries use java.net.HttpURLConnection.
The easier way. If you can add libraries use the Jersey client API. http://jersey.java.net/nonav/documentation/latest/user-guide.html#client-api
Java EE 7 will include an official client API, EE 6 only included the REST server-side API.
But you should prefer the XML content over the JSON content yourself. JSON is great because it is easy for JavaScript to parse. Java has more ways to parse XML than it does JSON. If you really want to use JSON you could look at something like http://jettison.codehaus.org/

If there is no JAVA API already written I would go for a JAX-RS approach with a client framework like jersey client. Look at http://jersey.java.net/nonav/documentation/latest/client-api.html.

Since you're creating a web app that needs to do HTTP request handling... Start with either Tomcat or Jetty, and Apache HTTP Client, and use a JSON library such as available from json.org.
If you are familiar with maven, you could get all this wrapped up and building within 10 minutes. Otherwise, you'll have to build the webapp and handle dependencies yourself.

If you are on Java EE 7 and want to use the included JAX-RS 2.0 API then have a look at https://github.com/tobiasdenzler/jee7-rest-crud. Its a simple CRUD project using JSON.

Related

Create json rest api without writing or integrating any code

I've 2 questions about Java Rest APIs
1'st:
I would like to create a Rest API with Java Servlets for using in the mobile applications(IOS and Android)
and before doing that I would like to clarify the rest api content. However as much as I search through google all I found was automatic API creators from your Java code. But what I want is different first I want to document our Json Rest API so mobile and server developers can start to work independently.
Do you know any tool for that?
2'nd:
What I want to achive in our java server application is simple. Get simple json requests from mobile clients and do some database query and respond back with simple json objects.
For achiving this do I need to use any additional Rest API framework such as spring mvc (or something else) or just using Java Servlets and parse the request in doPost method and respond it there
Which one do you suggest?
Thanks
Restlet Studio (http://restlet.com/technical-resources/restlet-studio) or APISpark (http://restlet.com/products/apispark/) can bring you what you expect.
Restlet Studio allows you to define / design you API with a Web IDE: resources and their methods, representations (exchanged data structures). You can then have access to corresponding Swagger content and generate online client SDKs and even server skeletons for your API (this is internally based on Swagger Codegen)...
To implement your RESTful applications, Restlet can help you. It's a Java REST framework to access and / or implement RESTful applications. Restlet can be used within a servlet container with its servlet extension (see this link https://github.com/restlet/restlet-tutorial/blob/master/modules/org.restlet.tutorial.markdown/02_Server_Side/04_Server_Deployment/02_Servlet_Deployment.md).
Hope it helps you.
Thierry
I aggree with Stephan comment.
With Spring Boot and Spring Data Rest, the only code you will need to write is the mapping between your DB and your DAO Entities.
To document your API before writing it, you can use in fact any regular tools.
REST APIs don't have anything specific compared to others APIs to be documented.
Much of the documentation of APIs I've written were using Ms-Word...
Swagger provide a Json syntax to document a REST API, but it is mainly useful when used with Swagger-Ui, which allow to request the deployed server dynamically. You can use Swagger for your documentation, but as there will be no already existing server, I'm not sure it is worth the cost of learning its syntax. The main benefits is to give you constraints (like predefined fields) to follow.
By the way, I think that writing a REST Api Mapping or just document it takes the same amount of time, so I'm not sure it is really worth making all the documentation in a stand-alone way.

How can I implement Hunch's API in java?

I'm creating a Java application that uses Hunch's API, but the problem is I have no clue as to how I can use a web API in Java. Any ideas?
That depends on what the API provides, generally you'd need a web server (like Tomcat) to run a web framework.
If it's just an api to communicate with a service via HTTP (maybe webservices) then have a look at the corresponding libraries (JAX-WS etc.).
Edit: I looked up Hunch API, and assume it is this: http://hunch.com/developers/v1/
It seems like it would return JSON encoded data, so you might use the URL class to read data from an URL. That data would be a JSON string which then needs to be decoded using one of the many JSON libraries out there (e.g. JSON-simple).

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 for handle Read HTTPRequst post data on Restful api

What is the best way of save data using Restful web service without using Ajax? As a example I need to add a new Customer to the database using submit button.
What is the best way of transfer data format (text,json,xml) ?
How to read POST or GET data from HttpRequest object?
If you can please give me a example in java .
Thank you
I think you need to separate the concepts a bit. A "Restful Web Service" is a web service designed using REST principals, whereas AJAX is a set of technologies used often on the client side for asynchronous requests to multiple resources (without fully reloading the page). The web service really shouldn't care how the HTTP request is generated, just the contents of the HTTP request.
Now if you're concerned about writing a rest service in Java, I would highly recommend looking into JAX-RS and the reference implementation Jersey. There are lots of examples of how to get up and running. You can use MessageBodyReader implementations are to convert data from the HTTP request entity into Java objects.
Obviously this is not the only way to get started with writing a Restful web service in Java, but is one way.
It's very definitely worth your time to carefully study Richardson and Ruby's RESTful Web Services to learn the REST architectural style. In addition to #ach_l's recommendation to use Jersey, take a look at the Restlet Java framework, which is completely wonderful.

Implementation of web service

I just want to implement a service in java that will:
take some arguments, then search the database
return the JSON object of the fetched data
I need help to identify the ways through which I can implement this thing.
e.g. Suppose I am getting the name of the book as argument I want to render.
On service part, I have to fetch book data and convert it to JSON and write/return to response.
I was looking at the Apache Axis2 but I am not sure that I am going in the right direction.
So, pls help.
Need guidelines not implementation.
Thanks
I would suggest using JAX-RS based services which would be ideal for your scenario as you want json data. These are pretty easy to get started with. Jersey is a widely used frameworks. Also see RESTEasy.
If you are returning the data in JSON then you probably don't need to implement a full web service, which uses XML for both the request and the response.
A normal dynamic web application (written as a Java Servlet) will be able to read request parameters in the HTTP payload and return a JSON-encoded HTTP response.
However you need to consider your clients; if they are only able to access web services then you need to forget about a JSON response and simply objectify the response. However if the clients can access web resources without issue then go with the servlet approach.
If you need to go with web services then look at the Metro 2 framework.
One way to do this is to keep it standards-based.
If you are using the JEE5/6 framework, your best bet would be to go with JAX-WS - comes built-in with the JSE too (if I remember correctly)
You really just have to annotate a POJO with #WebService to achieve this.
Regarding creating a JSON response, a good bet is to stick with the implementation from http://code.google.com/p/google-gson/ ; simple and straightforward
Axis2 can handle/support the webservice related part, iaw, transforming java objects into JSON and vice-versa and providing an easy-to-use API for the communication part.
Hibernate or JPA could be useful for database related tasks, although it might be easier to just use JDBC to send some simple SQL commands to the database (especially if the database already existst).

Categories