Archives

Swagger – Spring Boot

Step #1: Add maven dependency

                <dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.6.1</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.6.1</version>
		</dependency>

Step #2: Create a SwaggerConfig.java in com.janani.config

package com.revature.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

	@Bean
	public Docket api() {
		// @formatter:off
		return new Docket(DocumentationType.SWAGGER_2)
				.groupName("lms-api")
				.apiInfo(apiInfo())
				.select()
				.paths(PathSelectors.any()).build();
	}

	// @formatter:on
	// private Predicate<String> fetchPaths() {
	// return or(regex("/holidays.*"), regex("/roles.*"),
	// regex("/employees.*"));
	// }

	private ApiInfo apiInfo() {
		// @formatter:off
		return new ApiInfoBuilder()
				.title("LMS API")
				.description("Leave Management System")
				.license("LMS License")
				.contact("jananise@gmail.com")
				.licenseUrl("https://github.com/JananiVelmurugan")
				.version("1.0").build();
	}

}

Step #3: http://localhost:5000/swagger-ui.html#/

Spring demo – Sequence

Step 1: create maven web-app
Step 2: delete web.xml
Step 3: add dependency in pom.xml
Step 4: Spring Boot -> Application.java ( Run as -> Java Application )
Expected: application started
Step 5: Home Controller(with print statements) + index.jsp
Expected: Print statement given inside HomeController
Step 7: Pass parameters in controller

Spring JdbcTemplate – Calling Procedure

Procedure

DROP PROCEDURE IF EXISTS pr_check_menu_availability;

DELIMITER $$

CREATE PROCEDURE pr_check_menu_availability(IN in_name VARCHAR(20), OUT out_status VARCHAR(20))
BEGIN
 SET out_status='Not Available';
 IF in_name IN(SELECT NAME FROM menu)
 THEN 
  SET out_status='Available';
 END IF;
 END $$
 
 DELIMITER;
 
 CALL pr_check_menu_availability('Idli',@status);
 
 SELECT @status;

DAO

public String checkMenuAvailability(String name) {
		SimpleJdbcCall call = new SimpleJdbcCall(jdbcTemplate).withProcedureName("pr_check_menu_availability")
				.declareParameters(new SqlParameter("in_name", Types.VARCHAR),
						new SqlOutParameter("out_status", Types.VARCHAR));
		call.setAccessCallParameterMetaData(false);
		SqlParameterSource in = new MapSqlParameterSource().addValue("in_name", name);
		Map<String, Object> execute = call.execute(in);
		String status = (String) execute.get("out_status");
		return status;

	}

TestDAO

MenuDAO dao = new MenuDAO();
String status = dao.checkMenuAvailability("idli");
System.out.println(status);

Spring JdbcTemplate – Single/Multiple Column Result

Single Column Result

Find The Number of Menu Items
DAO

public Long findCount() {
Long menuCount = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM menu", Long.class);
return menuCount;
}

TestDAO

MenuDAO dao = new MenuDAO();
Long count = dao.findCount();
System.out.println(count);

Multiple Column Result

List of Menu Names
DAO

public List<String> findMenuNames() {
List<String> menuNameList = jdbcTemplate.queryForList("SELECT name FROM menu", String.class);
return menuNameList;
}

TestDAO

List<String> menuNames = dao.findMenuNames();
for (String menuName : menuNames) {
	System.out.println(menuName);
}

Spring JdbcTemplate – Call Function

Function

DROP FUNCTION IF EXISTS fn_is_menu_present;

DELIMITER $$

CREATE FUNCTION fn_is_menu_present(in_name VARCHAR(20))
RETURNS BOOLEAN
    
    BEGIN
	RETURN EXISTS(SELECT 1 FROM menu WHERE NAME=in_name);
    END$$

DELIMITER ;

SELECT fn_is_menu_present('idli');

DAO

public Boolean validateMenu(String name) {
String sql = "select fn_is_menu_present(?)";
Boolean isAvailable= jdbcTemplate.queryForObject(sql, new Object[] { name },    Boolean.class);
return isAvailable;
}

TestDAO

MenuDAO dao = new MenuDAO();
Boolean isValid = dao.validateMenu("idli");
System.out.println(isValid);