Restful Service using Spring MVC

A simple Restful service built by Spring MVC. Use Spring-Boot-initializer to initialize the project.
Tutorial can be found on https://www.youtube.com/watch?v=QHjFVajYYHM.

What is JPA?

Java Persistence API. Mapping Java objects to database tables and vice versa is called /Object-relational mapping/ (ORM). The Java Persistence API (JPA) is one possible approach to ORM. Via JPA the developer can map, store, update and retrieve data from relational databases to Java objects and vice versa.

Annotations

  • @Entity
  • @Id
  • @GeneratedValue (Automatically generate property in DB)
  • @Transient (not stored in DB)

Example

1
2
3
4
5
6
7
8
9
10
@Entity
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String summary;
public String getSummary() ...
public void setSummary(String summary) ...
...
}

Lombok

Automatically generates getters, setters, toString() methods for the fields in a Java class.

Repository support

public interface AccountRepository extends JpaRepository<Account, Long> { … }
Defining this interface serves two purposes: First, by extending JpaRepository we get a bunch of generic CRUD methods into our type that allows saving Accounts, deleting them and so on. Second, this will allow the Spring Data JPA repository infrastructure to scan the classpath for this interface and create a Spring bean for it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@Repository
@Transactional(readOnly = true)
class AccountServiceImpl implements AccountService {

@PersistenceContext
private EntityManager em;

@Autowired
private AccountRepository repository;

@Override
@Transactional
public Account save(Account account) {
return repository.save(account);
}

@Override
public List<Account> findByCustomer(Customer customer) {

TypedQuery query = em.createQuery("select a from Account a where a.customer = ?1", Account.class);
query.setParameter(1, customer);

return query.getResultList();
}
}

H2

H2 is a in-memory database. Here use BootStrapData to initialize data in H2.

Controller

Use annotation @RestController, @RequestMapping.
Important to add @Service to implementation class so that the controller can distinguish.

Get

@GetMapping(“/{id}”)
Use @PathVariable to obtain variable value.

Post

@PostMapping(“…”), @ResponseStatus(HttpStatus.Created)
Use @RequestBody to obtain body value.