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).
Related
I have two WEB applications based on two different servers. How I can pass data from the PHP web form, which consists of 5-10 fields, to Java application(Struts2, Spring2) in the most elegant and safe way?
I think cURL should be enough, you can make HTTP(S) requests from one to the othe server.
let the PHP return a JSON object, best would be RESTfull.
let the java code make a REST call
One answer: API. That's what APIs are intended for - communication between applications.
Create endpoint in the JAVA app that acceps the data and does something with it.
In PHP after recieving the data from form make a call to the JAVA app endpoint and send the data there.
For security you may want to use some data encription using oauth (that is if you data requires that kind of security).
You have created an API which will return JSON array.
Call that API from java application and get JSON, decode JSON and use that.
I'm new to this and I need my Java program to send http or https requests on the different websites(for example, Amazon)
so it can get the HTML code and than I would be able to find information(price on some goods) that I need via Pattern class.
Did anybody faced with that task? Maybe you used JSON or other tools? Thank you.
It seems that Amazon have an API. You should using instead of trying to parse their website.
Regarding libs to call webservices in JAVA, you could use Retrofit.
There are several parts to what you are asking:
Constructing / determining what to include in the HTTP request
Issuing the actual HTTP request
Parsing the response
The first and last are dependent on the particular service / API you are invoking, though if the API response is in a standard format (e.g. JSON), there are libraries that can help you interpret the response (though exactly which fields in the response mean something to you, will depend on the particular API and your application). Issuing the HTTP request, itself, is something that can be done with a number of different libraries, including the builtin HttpURLConnection / URL classes, as well as third party libraries such as the Apache HttpComponents or the Google HTTP Java Client Library, the latter of which includes libraries for parsing common output formats, as well.
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.
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.
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).