HTTP GET Method – Ways to form GET URL

HTTP GET Method – Ways to form GET URL

In this article let us see the ways to form GET URL for http requests. We are going to use Spring Boot and Google Chrome Postman to verify the URL formation.

Common ways of forming GET url’s are by

  1. Path
  2. Body

Query: (Path)

When GET URL is formed along with query, the URL will look like,

http://example/{id}

Params: (Body)

When GET URL is formed along with Params, we will send the parameters along with the body.

http://example?id=1

There are four parameters with request param,

  • defaultValue – This is the default value, if the request is not having the value or it is empty.
  • name – Parameter name
  • required – true, if we want to make the parameter required one.
  • value – value of the parameter

Now let us see an example – Various ways to form GET URL.
I have created a simple springboot application with a controller. The output shown below are from chrome postman application.

you can download the code here

Output:
/javainfinite

 

/javainfinite/{id}


/javainfinite/idParam

/javainfinite/params

/javainfinite/uriComponentBuild

/javainfinite/uriBuild

 

Download here

 

Read More

Spring Boot Actuators – Customizing Actuator Endpoint

Spring Boot Actuators – Customizing Actuator Endpoint

SpringBoot – Actuators (Example here)
Spring Boot Actuator brings in several production grade services to your application. Spring Boot Actuators has various number of built-in endpoints.

Actuators helps us monitor and interact with your application. Spring Boot also lets us to create our own endpoints too. These end points can be both enabled or disabled.

We have already seen in-build actuator endpoints here – Spring Boot Actuators – in-built EndPoint.

Let us see how to customize the already existing endpoint. Let us override the /actuator/health endpoint.

To override the health endpoint we will be extending a class – AbstractHealthIndicator

AbstractHealthIndicator:
Class which has base health indicator implementations and encapsulates creation of health and error handling. This will override the method – protected abstract void doHealthCheck(Health.Builder builder) throws Exception.

Let us see an example,

pom.xml

application.properties

ControllerSample.java

App.java

Now without customizing endpoint, if we run the application

Now let us override the health endpoint,

Output after overriding the healthIndicator,


Download this example here

 

 

 

Read More

Spring Boot Actuators – How to use it in application

SpringBoot – Actuators
Spring Boot Actuator brings in several production grade services to your application. Spring Boot Actuators has various number of built-in endpoints.

Actuators helps us monitor and interact with your application. Spring Boot also lets us to create our own endpoints too. These end points can be both enabled or disabled.

Below are some of the Spring Boot Actuator endpoints,

EndPoint Description
/actuator/health Basic health information of the application
/actuator/configprops list of all @ConfigurationProperties
/actuator/info Arbitrary Information
/actuator/auditevents audit events of the application
/actuator/httptrace HTTP trace information
/actuator/metrics Metrics information of the application
/actuator/mappings @RequestMappings in the application
/actuator/scheduledtasks Scheduled tasks in the application
/actuator/shutdown Shutdowns the application

Let us write a sample application to test the spring boot actuator end points.

pom.xml

add this property in application.properties,

application.properties

SampleController.java

App.java

Let us start the application,

 

Once the application is started, we can see the actuators are getting initialized. when we hit localhost:8080/actuator/{endpoint} we can see the information

Here are some sample endpoint outputs,

/actuator/health

/actuator/mappings

/actuator/httptrace

Download Example here

Read More

@Scope – How to get Scope of Bean from Code

@Scope – How to get Scope of Bean from Code

When we create a Bean we are creating  actual instances of the class defined by that bean definition. We can also control the scope of the objects created from a particular bean definition.

There are 5 types of scopes in bean,

  • singleton (default scope)
  • prototype
  • request
  • session
  • global-session

Singleton:
Single instance per spring IoC container

Prototype:
Single bean definition to any number of object instances.

Request:
Bean definition for each request. Only valid web-aware Spring ApplicationContext.

Session:
Bean definition for a session. Only valid web-aware Spring ApplicationContext.

Global-Session:
Similar to session but the only makes sense in the context of portlet-based web applications. Only valid web-aware Spring ApplicationContext.

In this example, we are going to see how can we get the Scope of bean through java code. We are going to create two bean classes – Employee and Student

Employee is Singleton and Student is Prototype.

Now let us see how to get these scope of bean through Java code.

 

pom.xml

Employee.java

Student.java

ScopeService.java

ScopeController.java

App.java

application.properties

home.jsp

Output:

From output we can see,
@org.springframework.context.annotation.Scope(proxyMode=DEFAULT, value=singleton, scopeName=) 

scopeName and value are aliases, we cant mention both together.

If we try to mention both, we will get the exception

Example:
@Scope(value=”singleton”, scopeName=”ScopeName”)

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.javainfinite.App]; nested exception is org.springframework.core.annotation.AnnotationConfigurationException: In AnnotationAttributes for annotation [org.springframework.context.annotation.Scope] declared on class ‘com.javainfinite.model.Employee’, attribute ‘value’ and its alias ‘scopeName’ are declared with values of [singleton] and [ScopeName], but only one is permitted.
But we can use either one @Scope(value=”singleton”) or @Scope(scopeName=”singleton”)

Download code here

 

 

 

Read More

SpringBoot – Custom Banner / Disable Banner

SpringBoot – Custom Banner / Disable Banner

Whenever we deploy the springboot application, it comes with default banner

We have an option to change this banner or even to disable this banner.

In order to disable this banner, we have to include a property in application.properties file

spring.main.banner-mode=off

If we include this property in application.properties file, SpringBoot banner will be disabled.

 

We can also customize the springboot banner as per our requirement. All we have to do is, Put the design required in a file and save it as banner.txt and paste it in resources folder along with application.properties.

Lets paste this design in banner.txt file,

Paste the banner.txt with application.properties in resources folder

Let us see the output,

Read More