Now you want your API return real data.
For test purpose I will implement the getAllStudents()
with hard coded data first.
public ResponseEntity<List<Student>> getAllStudents() {
List<Student> students = new ArrayList<>();
Teacher teacher = new Teacher();
teacher.setId(1L);
teacher.setFirstname("Great teacher");
teacher.setLastname("Onizuka");
Student student = new Student();
student.setId(1L);
student.setBirthday(LocalDate.now());
student.setFirstname("Anthony");
student.setLastname("Viard");
student.setTeacher(teacher);
students.add(student);
return new ResponseEntity<>(students, HttpStatus.OK);
}
Restart the application and test another time through swagger-ui.
The API work as expected. Let link the service with a database.
The JDL is a JHipster-specific domain language where you can describe all your applications, deployments, entities and their relationships in a single file (or more than one) with a simple and user-friendly syntax.
Using a JDL file will allow us to get all the entities and configuration ready as quick as possible.
For this workshop, the entities definitions are quite close than the API. Note that it will probably not the case for real use cases. We should not expose our entities via the API. The objects return by the API are, by definition, not the entities.
Consider this JDL
entity Student {
firstname String
lastname String
birthday LocalDate
}
entity Teacher {
firstname String
lastname String
}
relationship OneToMany {
Teacher to Student
}
Create a new file in your project root, named workshop.jdl
and copy/paste the JDL content.
Copy and paste this command in a terminal at the root folder project. Answer yes if there is some files conflict to overwrite.
jhipster import-jdl ./workshop.jdl
JHipster should found 2 entities and generate them.
INFO! The JDL is being parsed.
INFO! Found entities: Student, Teacher.
INFO! The JDL has been successfully parsed
INFO! Generating 2 entities.
After the process finished, restart your application.
After the complete restart, log with the admin account, navigate into the entities menu, 2 new entries should be available.
Click, on the student one. JHipster has created CRUD UI for our entities. By default in dev profile, the FakerJS library add random data. Enough for demo purpose.
Open the StudentsApiDelegateImpl
class, remove the hard coded student and call the StudentRepository, map entities to
API objects.
public ResponseEntity<List<io.github.openapiworkshop.web.api.model.Student>> getAllStudents() {
return new ResponseEntity<>(MapStudentEntityListToStudentApiList(studentRepository.findAll()), HttpStatus.OK);
}
private List<io.github.openapiworkshop.web.api.model.Student> MapStudentEntityListToStudentApiList(List<Student> students){
return students.stream().map(student -> {
io.github.openapiworkshop.web.api.model.Student studentApi = new io.github.openapiworkshop.web.api.model.Student();
studentApi.setId(student.getId());
studentApi.setFirstname(student.getFirstname());
studentApi.setLastname(student.getLastname());
if(null != student.getTeacher()) {
Teacher teacher = student.getTeacher();
io.github.openapiworkshop.web.api.model.Teacher teacherApi = new io.github.openapiworkshop.web.api.model.Teacher();
teacherApi.setId(teacher.getId());
teacherApi.setLastname(teacher.getLastname());
teacherApi.setLastname(teacher.getFirstname());
studentApi.setTeacher(teacherApi);
}
return studentApi;
}).collect(Collectors.toList());
}
Nb. This code snippet could be improved by externalize the mapping part in a specific class and avoid to call both API and Domain classes with sames name.
Restart the application.
Your swagger should now return data from your databases (here data generated from fakerJS).
You could use the default JHipster UI to add, remove or update your students and retrieve them from the API.
Let's consume this API in another application! >>> next