Posted on Dec 2nd 2014

With MyEclipse 2015, we’ve made some fantastic improvements to our REST tooling. We’ll use MyEclipse to create, explore and test a simple REST web service that allows us to add books to a library.

Step 1 - Creating your project

Let’s get started by creating a Java EE 6 REST Web Service Project in MyEclipse. We’ll use Jackson to convert objects to/from JSON, so be sure to select Jackson libraries during project creation like below.

select_libraries

Add the following init-params in web.xml for Jersey to support Jackson object mapping.

init_params

Step 2 - Essential snippets

Let's create a simple POJO named Book to represent books in our library.

public class Book {
String author;
String title;

public Book() {
}

public Book(String title, String author) {
this.title = title;
this.author = author;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

@Override
public String toString() {
return "Book [title=" + title + ", author=" + author + "]";
}
}

And let’s create a simple REST service for our Book object. Remember that you can use our REST class and method wizards to help you create REST endpoints.

@Path("/library/book")
public class BookService {

public static HashMap<Integer, Book> bookMap = new HashMap<Integer, Book>();

static {
// Add a default Book to the map
bookMap.put(1, new Book("War and Peace", "Leo Tolstoy"));
}

@GET
@Path("listbooks")
@Produces(MediaType.APPLICATION_JSON)
public String getAllBooks() {
String bookList = "";
try {

ObjectMapper mapper = new ObjectMapper();

// convert map to JSON string
bookList = mapper.writeValueAsString(bookMap);

} catch (Exception e) {
e.printStackTrace();
}

return bookList;

}

@GET
@Path("{id}")
@Produces({ "application/json", "text/plain" })
public Response getBook(@PathParam("id") Integer id) {
if (bookMap.containsKey(id)) {

return Response.ok(bookMap.get(id), MediaType.APPLICATION_JSON)
.build();
} else {

return Response.status(404)
.entity("A Book with id " + id + " cannot be found.")
.build();
}

}

@POST
@Path("create")
@Consumes(MediaType.APPLICATION_JSON)
public Response createBook(Book book) {

// Adds the new book to the hashmap
bookMap.put(bookMap.size() + 1, book);
String result = "Book saved: " + book;
return Response.status(201).entity(result).build();
}
}

Notice the MyEclipse Explorer view displays a REST Content node that reveals all REST elements in your project.
me_explorerAdditionally, the REST Project Explorer view provides a similar REST-centric outline for your REST projects.
rest_proj_explorer

To simplify navigation and editing, double-click any node in the REST Content or REST Project Explorer, and the corresponding REST class opens automatically in the editor. Any changes made in the editor are immediately synchronized in both views.

When you’re constantly dealing with a large number of REST resources and endpoints, you’ll find this project level outline invaluable.
navigationDeploy the project to the MyEclipse Tomcat server. Look for output like below in the Console.
console_output

Step 2 - Testing the service

Now, for the fun part! Let's test the service in our new REST Explorer. Unlike the explorer in earlier versions of MyEclipse, this one has a ton more features and works on all OSs and architectures, including Windows 64.

Starting the explorer is pretty simple. Right-click your project and choose MyEclipse>Test with Rest Web Services Explorer. This opens a WADL file corresponding to your project in the REST Explorer.
rest_explorer

Let’s take a closer look at each section of the explorer:

URL Bar
The URL bar automatically points to the WADL file of the project. You can edit this section to enter a custom URL that you want to test.

Outline
The Outline section displays a structure for the service. Selecting a node reveals the details on the other side of the editor.

Source Tab
The editor also has a Source tab where you can view the WADL source.

wadl_source

Test GET
From the Outline, by selecting /listbooks/getAllBooks and then clicking Test method, the method is opened in a new tab. When you click the Execute button, the request sent to the server looks like:
raw_requestAnd the response looks like:

Test Post
Let’s add a book to our library by selecting the createBook endpoint:
test_post

Response:
post_response

Test @PathParam
Let's get the book we just created using the book ID with the @PathParam annotation. For such annotations, the rest explorer automatically adds an user input field where values can be entered and tested.

get_request

Response:
get_response
As you can see, creation, setup and testing of REST services can’t get any easier. We hope you enjoyed this brief dive into our new REST capabilities.

Have questions about this walk-thru? Send our MyEclipse Team an email to sales@genuitec.com or post directly on our forums. 

About the Author
Author Photo Srivatsan is the MyEclipse QA Manager and has been part of the MyEclipse Team for over seven years. By working in MyEclipse development and then moving to QA, Srivatsan has acquired a vast, unique perspective on all things MyEclipse.