Skip to main content

Generate Documentation for Spring Boot API with swagger and Open API 3

 ðŸ˜‚ 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

Popular posts from this blog

How to Send OTP in Mobile Number | login with OTP mobile Number | How to send OTP in mobile no with Spring Boot APP

   ðŸ˜‚               Login with Mobile Number OTP ---------------------------------------------------------------------------- If you want to develop a project to log in with OTP mobile Number in Spring Boot Applications then this post for you. In this post, I am going to use some other service to send the OTP in mobile number. we have to use it in this project spring boot. we are going to use Twilio to send SMS. we are going to use a web socket to send the data from the browser to the SMS gateway. Oracle Database for store the user details. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <version>2.3.3.RELEASE</version> </dependency> <dependency> <groupId>com.twilio.sdk</grou

Spring Boot With MySQL Database connection with Examples | MySQl Database Configuration with Spring Boot Projects

 ðŸ˜ƒ MySQL Database Configuration with Spring Boot Projects  In this article, we are going to introduce How to connect MySQL Database with the Spring Boot project. pom.xml   <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</gro

How can we create Auto generated field or ID for mongodb using spring boot

😂 How can we create an Auto-generated field or ID for MongoDB using spring boot? First Create One Application Like Mongodb_sequence-id-generator Pom.XML <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency> <groupId>de.flapdoodle.embed</groupId> <artifactId>de.flapdoodle.embed.mongo</artifactId> </dependency> User.java package com.app; import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Transient; import org.springframework.data.mongodb.core.mapping.Document; @Document(collection = "users_db") public class User { @Transient public static final String SEQUENCE_NAME = &