Saturday, June 20, 2015

Spring Boot Angular Part 2

In part 2 of the Spring Boot Angular series, we are going to add some RESTful services. We'll first start off by creating two new packages.  One package will be controller and the other will be dtos under com.herzog.boot.  We are going to create two RESTful APIs.  One will be to retrieve a list of albums and the other will be to retrieve information about a specific album.  Retrieving a list of albums will be a HTTP POST and retrieving an album will be a HTTP GET so we can have an example of both.  We'll first start with retrieving a list of albums by creating request and response objects.  We want to search for a specific artist so the request object will have a artist attribute.

package com.herzog.boot.dtos;

public class AlbumListRequest {

 private String artist;

 public String getArtist() {
  return artist;
 }

 public void setArtist(final String artist) {
  this.artist = artist;
 }

}

The response object is just going to return a list of strings for now.  We are not going to hook up to the iTunes API just yet.


package com.herzog.boot.dtos;

import java.util.List;

public class AlbumListResponse {

 private List albums;

 public List getAlbums() {
  return albums;
 }

 public void setAlbums(final List albums) {
  this.albums = albums;
 }

}


For retrieving an album, we are going to use an HTTP GET so we will only have a response object.  We'll use a path variable for the request.  The album response object will just return the name and the number of tracks in the album.

package com.herzog.boot.dtos;

public class AlbumResponse {

 private String name;
 private int tracks;

 public String getName() {
  return name;
 }

 public void setName(final String name) {
  this.name = name;
 }

 public int getTracks() {
  return tracks;
 }

 public void setTracks(final int tracks) {
  this.tracks = tracks;
 }

}

Now that the DTOs have been created, it is time to create the interface for the RESTful APIs.  We'll have two methods.  The list method will be used to retrieve a list of albums and the find method will be used to retrieve information about a specific album.

package com.herzog.boot.controller;

import com.herzog.boot.dtos.AlbumListRequest;
import com.herzog.boot.dtos.AlbumListResponse;
import com.herzog.boot.dtos.AlbumResponse;

public interface AlbumController {
 
 /**
  * Return a list of albums by a specific artist
  * 
  * @param request AlbumListRequest
  * @return AlbumListResponse
  * @throws Exception - throw all exceptions
  */
 public AlbumListResponse list(final AlbumListRequest request) throws Exception;
 
 /**
  * Return the properties of a specific album
  * 
  * @param id - Album ID
  * @return AlbumResponse
  * @throws Exception - throw all exceptions
  */
 public AlbumResponse find(final int id) throws Exception;


}

Lastly, we need to create the implementation class.  We'll leverage all of Spring's MVC components.  If you have worked with REST in Spring before, this should all look familiar to you.  If you haven't, I recommend that you look into Spring MVC.  I will not go into that since it is out of the scope of this tutorial.

package com.herzog.boot.controller;

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.herzog.boot.dtos.AlbumListRequest;
import com.herzog.boot.dtos.AlbumListResponse;
import com.herzog.boot.dtos.AlbumResponse;

@Controller("albumController")
@RequestMapping(value = "/api/album", consumes = "application/json", 
 produces = "application/json")
public class AlbumControllerImpl implements AlbumController {
 
 @Override
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
 public AlbumListResponse list(@RequestBody final AlbumListRequest request) throws Exception {
  
  AlbumListResponse response = new AlbumListResponse();
  List albums = new ArrayList();
  for (int i = 0; i < 10; i++) {
   albums.add(request.getArtist() + " Album " + i); 
  }
  response.setAlbums(albums);

        return response;
    }

 @Override
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
 public AlbumResponse find(@PathVariable(value = "id") final int id) throws Exception {
  
  AlbumResponse response = new AlbumResponse();
  response.setName("Album " + id);
  response.setTracks(10);
  
  return response;
 }

}

If you have worked with the Spring framework before, you are probably thinking what happened to all the configuration steps.  That is the beauty of Spring Boot.  All the configuration steps are done for you by default by Spring Boot.

Now it is time to build and run the application. We can build the application by running the following command:

 mvn package

Lastly, run the application:

 java -jar target/spring-boot-angular-0.0.2-SNAPSHOT.war 

You'll need to use your favorite REST Client to invoke the RESTful services. Make sure that you set the HTTP headers for Accept and Content-type to application/json. The URLs for the two RESTful APIs we just created are as follows:

List Albums (POST) - http://localhost:8080/api/album/list
Retrieve Album (GET) - http://localhost:8080/api/album/:id

You can find the existing source code for part 2 at https://bitbucket.org/davidjherzog/spring-boot-angular.  The code is under branch 0.0.2-SNAPSHOT.

In Part 3 of this series, we will add some RESTful services.

No comments:

Post a Comment