addressListRoute


layout: default title: Demo: Address List Route


Up to this point we haven't interacted with a database using Spring REST. Remember the JPAVideoStore project we created during JPA week? We are going to use this pre-configured backend to create some resource routes for Address and Actor objects.

  1. Find your JPAVideoStore project. Run one of your JUnit tests to make sure that your connections are still up and running.
  2. If you broke or lost this project, use the provided solution in the resources directory.

  3. Create a separate Spring Starter Project called VideoStoreREST.

  4. Walk through the standard process of creating a Spring Boot project.
  5. Give your project a base package of com.example.
  6. Set Type to Gradle, Java Version to 8, and Packaging to War.
  7. Be sure to include the Spring Web, Spring Data JPA, and MySQL Driver dependency bundles.

  8. Edit the new project's settings.gradle file at the same level as your build.gradle file. Add JPAVideoStore as a sub project.

    rootProject.name = 'VideoStoreREST'
    
    includeFlat 'JPAVideoStore'
    

  9. Add your JPAVideoStore project as a dependency to the build.gradle file, then run a Gradle refresh.

    dependencies {
         implementation project(':JPAVideoStore')
    
       // ...
    }
    

  10. Configure your port and datasource in the application.properties.

    #### PORT CONFIG ####
    server.port=8081
    
    #### MYSQL DATASOURCE ####
    spring.datasource.url=jdbc:mysql://localhost:3306/sdvid?useSSL=false&&useLegacyDatetimeCode=false&serverTimezone=US/Mountain
    spring.datasource.username=student
    spring.datasource.password=student
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    

  11. Create two packages, one called com.example.videostorerest.data and the other called com.example.videostorerest.controllers.

  12. Create files called AddressDAO, AddressDAOImpl, and AddressController in their respective packages.

  13. Annotate your DAOImpl as @Transactional and @Service. Include the EntityManager as a field.

    @Transactional
    @Service
    public class AddressDAOImpl implements AddressDAO {
      @PersistenceContext
      private EntityManager em;
    }
    

  14. Annotate the controller as a @RestController and @Autowire in your AddressDAO. Give the class a @RequestMapping so that all the subsequent routes are preceded by api/.

@RestController
@RequestMapping("api")
public class AddressController {
  @Autowired
  private AddressDAO addressDAO;
}
  1. Write a single DAO method that uses JPQL to query the database to retrieve and return a list of addresses.
public List<Address> findAll(){
  String query = "Select a from Address a";
  return em.createQuery(query, Address.class).getResultList();
}
  1. Back in your controller, write a method that is mapped by the path /api/addresses which returns the result of calling the DAO method we just wrote.
@RequestMapping(path="addresses", method=RequestMethod.GET)
public List<Address> index(){
  return addressDAO.findAll();
}
  1. Run the program and hit the route http://localhost:8080/api/addresses in Postman. View the returned json data.

Prev -- Up -- Next