Skip to content

Commit

Permalink
Updated api yaml file and added ReadMe.md file
Browse files Browse the repository at this point in the history
  • Loading branch information
sainik73 committed Jan 17, 2021
1 parent 1f83e85 commit 2f84107
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Welcome to students-api

This repository shows the working example of how to design and build an API using the API First Design approach.
The Design First approach advocates for designing the API's contract first before writing any implementation/code.

Summary
-----------------
Open API spec File 'students-api.yaml' [located under 'src/main/resources']

Any modification to students-api.yaml file, necessitates the build of the project as per below instructions to
regenerate the API and Model classes. This is done using the swagger-codegen-maven-plugin.
REST Controller 'StudentsApiController' consumes these generated classes.


Instructions
-----------------

1. Clone this repository:

`git clone https://github.com/sainik73/students-api`

2. Build the project with Maven:

```
cd students-api/
mvn clean install
```

3. Run the spring boot application 'StudentsApplication'
4. Test the application using postman
[![Run in Postman](https://run.pstmn.io/button.svg)](https://app.getpostman.com/run-collection/04fec31e35710e9bdb41#?env%5Bstudents-api-local-env%5D=W3sia2V5IjoiYmFzZVVybCIsInZhbHVlIjoibG9jYWxob3N0OjgwODAiLCJlbmFibGVkIjp0cnVlfV0=)
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@


import com.example.openapi.StudentsApi;
import com.example.openapi.StudentsByAgeApi;
import com.example.openapi.model.Student;
import com.example.openapi.model.Students;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@RestController
public class StudentsApiController implements StudentsApi {
public class StudentsApiController implements StudentsApi , StudentsByAgeApi {
@Override
public ResponseEntity<Students> studentsGet(String studentName) {
//call DB to get resources by studentName
Expand All @@ -20,15 +24,15 @@ public ResponseEntity<Students> studentsGet(String studentName) {
s1.setName(studentName);

Student s2 = new Student();
s2.setStudentid(101);
s2.setAge(23);
s2.setStudentid(102);
s2.setAge(22);
s2.setName(studentName);

Students studentsList = new Students();
studentsList.add(s1);
studentsList.add(s2);

return new ResponseEntity<Students>(studentsList,HttpStatus.OK);
return new ResponseEntity<>(studentsList, HttpStatus.OK);
}

@Override
Expand All @@ -54,4 +58,26 @@ public ResponseEntity<Student> studentsStudentIdGet(Integer studentId) {

return new ResponseEntity<>(s,HttpStatus.OK);
}
}

@Override
public ResponseEntity<Students> studentsByAgeGet(@NotNull @Valid Integer studentAge) {
//call DB to get resources by studentAge
//for now, mock the call
Student s1 = new Student();
s1.setStudentid(101);
s1.setAge(studentAge);
s1.setName("Alex");

Student s2 = new Student();
s2.setStudentid(102);
s2.setAge(studentAge);
s2.setName("Robert");

Students studentsList = new Students();
studentsList.add(s1);
studentsList.add(s2);

return new ResponseEntity<>(studentsList,HttpStatus.OK);
}

}
12 changes: 5 additions & 7 deletions src/main/resources/students-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ paths:
$ref: '#/components/schemas/Students'
"404":
description: Student not found in system
put:
description: upate a student record
post:
description: Add a new student
requestBody:
content:
application/json:
Expand All @@ -50,15 +50,15 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error'
post:
description: Add a new student
put:
description: upate a student record
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Student'
responses:
"201":
"200":
description: Successfully created a new student
"400":
description: Bad request
Expand Down Expand Up @@ -90,8 +90,6 @@ paths:
$ref: '#/components/schemas/Student'
"404":
description: Student not found in system
/student:
description: Student resource
/students-by-age:
get:
description: fetch student by age
Expand Down

0 comments on commit 2f84107

Please sign in to comment.