Add APIs

Since we will serve the UI from http://localhost:8080/messages, it is better that we separate the path for APIs. Let's put API requests under /api/.

We will need to make the following changes to MessageController:

  • Change the existing /messages (POST) to /api/messages (POST) for saving new messages
  • Add /api/messages (GET) for retrieving messages
  • Remove the @RequestMapping annotation from the MessageController class level 

Here is the updated MessageController:

@Controller
public class MessageController {
...
@GetMapping("/api/messages")
@ResponseBody
public ResponseEntity<List<Message>> getMessages() {
List<Message> messages = messageService.getMessages();
return ResponseEntity.ok(messages);
}

@PostMapping("/api/messages")
@ResponseBody
public ResponseEntity<Message> saveMessage(@RequestBody MessageData data) {
...
}
}

The getMessages() handler for the /api/messages (GET) API is the new one we add here. Inside this method, we invoke MessageService.getMessages() to get messages.

Here are the changes to MessageService to add the getMessages() method:

public class MessageService {
...
@Transactional(readOnly = true)
public List<Message> getMessages() {
return repository.getMessages();
}

@SecurityCheck
@Transactional
public Message save(String text) {
return repository.saveMessage(new Message(text));
}
}

As you can see, we add the @Transactional annotation here to create a transaction because we are going to use sessionFactory.getCurrentSession() to get a Hibernate session in the repository. And, because it is just a read operation, we mark it as read-only. We also need to change the save() method of MessageService to remove the updateStatistics() method because we don't need it here.

Here is the change to MessageRepository:

public class MessageRepository {
...
public List<Message> getMessages() {
Session session = sessionFactory.getCurrentSession();
String hql = "from Message";
Query<Message> query = session.createQuery(hql, Message.class);
return query.list();
}
}

In the getMessages() method, after getting a Hibernate session, we create a Hibernate Query Language (HQL), to retrieve all of the messages. Hibernate will translate this HQL into an SQL similar to the following and send it to the database:

select message0_.id as id1_0_, message0_.created_date as created_2_0_, message0_.text as text3_0_ from messages message0_
You can open the debug log of Hibernate SQL as follows to print out the SQL that Hibernate generates: logging.level.org.hibernate.SQL=DEBUG

To execute the HQL, we need to create a Hibernate org.hibernate.query.Query object and call its .list() method to ask Hibernate to execute the query and return all of the records in the messages table. Hibernate will take care of the relational-to-object mapping and provide us with a list of Message objects.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.182.62