Tuesday, 24 September 2019

Enable HTTPS URL for Spring Boot wep application


Enable SSL certificate in spring boot application.
To enable https URL first we need to create an SSL certificate and configure the SSL certificate with spring boot application.


Create SSL certificate –
Navigate to the java installation directory, inside bin folder there is keytool executable file.
Run below command to generate jks file.
keytool -genkeypair -alias cert -keyalg RSA -keysize 2048 -keystore cert.jks -validity 3650
Run below command to get the content of keystore-
keytool -list -v -keystore cert.jks


Create a spring boot application–
Copy the certificate jks file in src/main/resources folder
Add below property in you application.yml file
server.port: 8443
server.ssl.key-alias: cert
server.ssl.key-store-password: ********
server.ssl.key-store: classpath:cert.jks
server.ssl.key-store-provider: SUN
server.ssl.key-store-type: JKS
Now you can access the url with https 
Refer below repository for project-
Enable SSL URL for web application

Friday, 20 September 2019

GraphiQL

GraphiQL is a query language, it’s expose a single endpoint API and we can fetch data from query as per the needs.
Graph QL can be used with any programming language and with any framework.
Grabh QL is more efficient and flexible in client-server communication.
A GraphQL service is created by defining types and fields on those types, then providing functions for each field on each type
Example:-
Create Graph QL using spring boot. Add below dependency.
dependencies {
    compile 'com.graphql-java-kickstart:graphql-spring-boot-starter:5.0.5'
     compile 'com.graphql-java-kickstart:graphiql-spring-boot-starter:5.0.5'
     compile 'com.graphql-java-kickstart:graphql-java-tools:5.3.4'
}
Add below mapping in application.yml
graphql:
  servlet:
    mapping: /api/graphql
    enabled: true

graphiql:
  enabled: true
  mapping: /graphiql
  endpoint:
    graphql: /api/graphql
  cdn:
    enabled: true
    version: 0.12.0
Create a graph QL query schema schema.graphqls
type Query {
  firstName: String!
  lastName: String!
}
Now create a query resolver class to handle above query, create one method for each query field
import org.springframework.stereotype.Component;
import com.coxautodev.graphql.tools.GraphQLQueryResolver;

@Component
public class RootQueryResolver implements GraphQLQueryResolver {

     public String getFirstName() {
           return "Manoj";
     }

     public String getLastName() {
           return "Kumar";
     }
}
Now run spring boot application and run below URL
This will open the graph QL tool in browser now you can pass the query
{
  firstName,
  lastName
}
Output:-
{
  "data": {
    "firstName": "Manoj",
    "lastName": "Kumar"
  }
}
Refer below screenshot-
Defining Object Fields in schema-
type Query {
  employee: Employee!
}
type Employee {
     EmployeeNo: String!
     Name: String!
}
In above example we have defined a field employee of type Employee object and then further declaration Employee type object with its fields. Now create a method in query resolver class getEmployee() which will return employee object.
To declare a list use below scheme-
type Query {
  employee: [Employee!]
}
type Employee {
     EmployeeNo: String!
     Name: String!
}
Defining more complex schema:-
type Query {
  employee: Employee!
}

type Employee {
     empId: String!
     firstName: String
     lastName: String
     age: Int
     addressList: [String]
}
Query resolver class-
import java.util.Arrays;

import org.springframework.stereotype.Component;
import com.coxautodev.graphql.tools.GraphQLQueryResolver;
import spring.boot.graphql.model.Employee;

@Component
public class RootQueryResolver implements GraphQLQueryResolver {

     public Employee getEmployee() {
           Employee employee = new Employee();
           employee.setEmpId("123");
           employee.setFirstName("Manoj");
           employee.setLastName("Kumar");
           employee.setAge(30);
           employee.setAddressList(Arrays.asList("Addrs1","addrs2"));
           return employee;
     }
}

Refer below screenshot for output