Java Spring boot framework

Spring Boot 

Here, We will discuss about java spring boot framework. Its widely used web framework where can be easily develop java web applications.

If we want to create web service without spring boot, we have to make many configurations before run it. But if we use spring boot framework, almost all of them are automatically  configure by spring boot. So we don't need to be worry about configurations. Just do coding. And when considering the coding level, It also really lesser than usual java coding. 

Just think, with spring boot we can write simple web application using approx: 3-4 coding lines!!!. How efficient that?

Here I will create a simple restfull web service using spring boot.
I will use Eclipse IDE for this.

First start maven project and select quick-start as a arctype.
Enter GroupId. You can give any name for that. I will enter mySpringBootProject
Enter ArtifactId. This also can be any name. I will enter springBootProject
Then click Finish.

It will create a sample maven project. Expand that and open pom.xml. It will looks like this

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>mySpringBootProject</groupId>
  <artifactId>springBootProject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>springBootProject</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>


Add this content to above pom.xml

     <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 

     <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
    </parent>


and Inside <dependencies></dependencies

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


Then go to directory which pom.xml located using commad prompt. And issue this command

mvn clean install

This will download some dependencies and build your web service. After successful build, It will show this output

 Then open app.java class and Modify it like this















Then create class named User.java













Then create a new class named myController.java. This class will manage rest requests and responces.













Now again go to directory which pom.xml located using Command Prompt and issue command before

mvn clean install

Then issue this command

java -jar target/springBootProject-0.0.1-SNAPSHOT.jar

It will show this mssage
Now our spring boot web application is succesfully running. We can access it using this url

http://localhost:8080/user

Enter above url, Then it will result this

{"name": "Kamal", "age":"25", "school":"High School"}

Comments

Popular posts from this blog

CSRF Defence - Synchronizer Token Pattern Demo

Lets read emails in gmail using oAuth 2.0 (Application demo)

How to view queries executed in MySQL?