Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST API for Java?

Tags:

java

rest

I am preparing an application which is console based and the outcome of the application is a RDF/XML file which contains data of all my connections from LinkedIn. Now the problem is that my entire application is console based and I need to have a REST API so as to incorporate with my application.

I am not aware of REST API's and how to use it with JAVA but can easily get through the documentation and understand it. My applications use the REST API of LinkedIn.

So can you please suggest some of the good REST API for Java?

like image 639
Shiv Kumar Ganesh Avatar asked Jun 24 '11 06:06

Shiv Kumar Ganesh


People also ask

What are REST API in Java?

Overview. A REST API (also known as RESTful API) is an application programming interface (API or web API) that conforms to the constraints of REST architectural style and allows for interaction with RESTful web services. REST stands for representational state transfer and was created by computer scientist Roy Fielding.

Can I use REST API in Java?

The code for REST APIs can be written in multiple languages but Java Programming Language due to its “write once run anywhere” quality is preferred for creating these APIs. This article will introduce you to the Java Programming Language and REST APIs along with their key features.

What is Java API vs REST API?

Correct, the Java API is internal, your code that uses that would be running inside a server. REST is the external interface. It is provided by internal code though, so there's some commonality there.


2 Answers

JAX-RS is the standard Java API for RESTful web services. Jersey is the reference implementation for this, it has server-side as well as client-side APIs (so, ways to expose methods in your code as RESTful web services, as well as ways to talk to RESTful web services running elsewhere).

There are also other implementations of JAX-RS, for example Apache CXF and JBoss RESTEasy.

like image 191
Jesper Avatar answered Oct 14 '22 00:10

Jesper


Quick code example:

1) Add the javax.ws.rs dependency in your pom (if using Maven) or download it.

    <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>jsr311-api</artifactId>
            <version>1.1.1</version> 
    </dependency>

2) Create an empty class to define the path of your service; for example for hearing at application/service/rest would be

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/service/rest")
public class WebConfig extends Application {
}

3) Create the controller of your api. For example if we need these calls: application/service/rest/resource/{id} a simple code would be:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;    

@Path("resource/{id}")
public class ApiController {

  /**
   * Call: <code>/service/rest/resource/23</code>
   * @return HTTP Response
   */
  @GET
  public Response getResource(@PathParam("id") String anId) {
    Resource myResource = whatever.get(anId);
    return Response.status(Status.OK).entity(myResource).build();
  }

4) If we want to specify a JSON response be sure you have the getters for your resource and type:

@GET
@Produces("application/json")
public Response getResource(@PathParam("id") String anId) {
   // the same
}
like image 39
Manu Avatar answered Oct 13 '22 23:10

Manu