😂 How to Generate Documentation for Spring Boot API project Make sure your Project having a swagger or open API 3 Dependency then only you get proper Documentation for your Service.
----------------------------------------------------------------------------
Every method or API above you have to Write Like that then only you get proper Documentation
If you do everything in your Application and Run your Application and go to browse and search.
.http://localhost:8080/v3/api-docs/
if you want to learn more go with this one. Open API-3
1. For Return List of Object then you have to use this API operation
@Operation(summary = "This will fetch List of Patient Detailas base on Hospital Name")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successfully fetch All Patient Details from Database ", content = {
@Content(mediaType = "application/json" ,array = @ArraySchema(schema =@Schema(implementation =PatientDetails.class)) ) }),
@ApiResponse(responseCode = "204", description = "No one Patient Data is avilable in Database", content = {
@Content })
})
@GetMapping("/patient-caretakers")
public ResponseEntity<?> patitentList(@RequestBody RequestData data){
System.out.println(data.toString());
List<PatientDetails> hospitalPatient=new ArrayList<PatientDetails>();
try {
System.out.println("phone");
List<PatientDetails> patientList=patientRepositery.findAll();
System.out.println("phone");
Iterator<PatientDetails> patientDetails=patientList.iterator();
System.out.println("phone");
while(patientDetails.hasNext()) {
PatientDetails patient=patientDetails.next();
System.out.println(patient.getHospitalName()+"|"+patient.getEmergencyContractNo());
String nameOfHospital=patient.getHospitalName();
String number=patient.getEmergencyContractNo();
if(nameOfHospital.equalsIgnoreCase(data.getHospitalName()) && number.equalsIgnoreCase(data.getPhoneNo())) {
hospitalPatient.add(patient);
}
}
}catch(Exception e) {
e.printStackTrace();
return new ResponseEntity<String>("No Record Found",HttpStatus.NO_CONTENT);
}
return new ResponseEntity<List<PatientDetails>>(hospitalPatient,HttpStatus.OK);
}
}
-----------------------------------------------------
2.For Return Single Object with particular service
@Operation(summary = "This Method is Use to validate the user from database and also provide the token")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successfully User is validate from Database and send the token with User Details ", content = {
@Content(mediaType = "application/json",schema =@Schema(implementation =JwtResponce.class)) }),
@ApiResponse(responseCode = "401", description = "Bad Credential User are requesting", content = {
@Content(mediaType = "application/json",schema =@Schema(implementation =String.class))}) })
@RequestMapping(value="/staff/login",method = RequestMethod.POST)
public ResponseEntity<?> generateToken(@RequestBody JwtRequest jwtRequest ) throws Exception{
System.out.println("Hello Controller");
System.out.println(jwtRequest.toString());
try {
System.out.println("Hi");
this.authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(jwtRequest.getUsername(),jwtRequest.getPassword()));
System.out.println("Hi");
}
catch(Exception e) {
e.printStackTrace();
throw new BadCredentialsException("Not Valid User");
}
//fine are go a head
CustomUserDetails userDetails =this.customuserdetailsservice.loadUserByUsername(jwtRequest.getUsername());
System.out.println("Hello token generater-1");
String token=this.jwtutil.generateToken(userDetails.getUser());
System.out.println("Hello token generater-2");
System.out.println(token);
//prepard Responce
StaffDetails u=userDetails.getUser();
UserDetUser user=new UserDetUser();
user.setId(u.getId());
user.setName(u.getUsername());
user.setEmail(u.getEmail());
user.setPhoneNo(u.getPhoneNo());
user.setRole(u.getRole());
user.setPhoneExt(u.getPhoneExt());
return ResponseEntity.ok(new JwtResponce(token,user));
}
}
------------------------------------------
3.you have to Write this thing for Spring Boot Main class
@OpenAPIDefinition(info=@Info(title = "Login Module ",version = "1.0.0",description = "This API for Login staff(ADMIN,DOCTOR,NURSE) and Patient_Caretatker"))
@SpringBootApplication
public class LoginModuleApplication implements CommandLineRunner{
@Autowired
private HospitalRepositery hospitalRepositery;
public static void main(String[] args) {
SpringApplication.run(LoginModuleApplication.class, args);
}
Comments
Post a Comment