Friday, June 9, 2017

Installation and Create Simple Spring Boot Project on Eclipse (EN)

Filled under:

The following are the simple tutorial how to build application using Spring Boot Framework.
This knowledge I have gotten when I studied Enterprise Programming in my second semester at my second college. The professor, Samuel Louvan who was really inspiring due to the easy explanation he made, then I can pass the lecturing as well.
I though the practical materials were really systematics.

Before understanding how to build application using Spring Boot Framework, it is better to know the fundamental of layering principal in programming, because Spring Boot Framework actually uses the proper concept. That is Three Tired Web Layers.

Three Tired Web Layers

You might know or not know about this concept. But let me show you how 3 layers develop the whole component of the application.
http://martinfowler.com/bliki/PresentationDomainDataLayering.html

  1. Presentation layer contains any program that will handle http request and render HTML page
  2. Domain layer contains any program that is commonly called as the business logic which also contains the validations and the calculations logic
  3. Data Access layer contains any program that sorts out how to manage persistent data in the database or remote services

Presentation Layer

This layer has responsibility to display the information to the user and interpret command from the user into actions upon the domain and data layer
There are several characteristics owed by the presentation layer such as:
  • How to handle interaction between the user and the software
  • this can be simple as a command-line or text-based menu system
  • These days, it's more likely to be a rich-client graphical user interface (GUI) or and HTML-based browser user interface

Data Source

This layer has responsibility to communicate with the other systems that carry out tasks on behalf of the applications.
There are also several characteristics owed by the Data source such as:
  • Communication with the database
  • Communication with the other applications
  • Communication with the Messaging System
  • Communication with the Transaction Managers

Domain Logic

This layer has responsibility determine the work of applications need to do for the domain we are working with.
There are several characteristics owed by Domain logic such as:
  • Also referred as Business Logic
  • It involves the calculations based on inputs and stored data
  • validation of any data that comes in from the presentation layer
  • and figuring out exactly what data source logic to dispatch
This domain logic created aim to really hide the data from the presentation layer, so that the user will never access the data directly.

How to separate these layers?

it depends on how complex the application is!
  • as the system gets more complex, we would break the three layers into separate classes
  • as complexity increased, we would divide the classes into separate packages
Make sure you do some kind of separaiton-at least at the subroutine level!

Rules

The domain and data source should never be dependent on the presentation!
It means there should be no subroutine call from the domain or data source code into the presentation code. because it will be easier for us to substitute with other/different presentations



OK! after this, we will create program using Spring Boot and Eclipse. Before, there are several requirement that you have to meet in the beginning such as:

  • Download JDK (Java Development Kit) here
  • Set Path JAVA_HOME like here
  • Install Eclipse which installed Maven inside, that is NEON version here
  • Install Spring Tool Suite (STS) here, or you can directly install by visiting Help -> Market Place
After the above required tools installed in your PC, you can start to build the application by following these steps:

Create New Spring Starter Project


  • Open Eclipse
  • Install STS through Eclipse Market Place
  • After finish installing STS, the system will ask you to restart Eclipse, then please restart Eclipse
  • To open Spring Boot project, choose menu File -> New -> Other...
  • Please choose Spring Starter Project within the folder Spring
  • Use default value from Eclipse to fill the properties

  • Click Next, then checklist Web dependency
  • Click Finish

Changing Port Number

  • As the default, server which is used by Spring Boot has port 8080. If you want to change the port number, you can simply change it by going to src/main/resource to access application.properties. Then, you can add the following code:

server.port = 9090

  • In the src.main.java, create new file HelloWorldController.java and add the following code
package com.example; 

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController public class HelloWorldController { 
   
@RequestMapping("/") 
   
public String index ()  { 
       
return "Hello World!"; 
   
} 
}


  • Run the project, Run As Java Application

Using Page Controller

  • Change pom.xml and add the following code:

dependency>   
   
<groupId>org.springframework.boot</groupId>
   
<artifactId>spring-boot-starter-thymeleaf</artifactId> 
</dependency>

  • Create the HTML page at src/main/resources/templates/hello.html
<!DOCTYPE   html>   <html   xmlns:th="http://www.thymeleaf.org">                   
   
<head>                                 
       
<title>First    Page</title>                   
   
</head>                
   
<body>                                 
       
<h1>Hello,  world!</h1>                
   
</body> 
</html>
  • Create Controller page for HTML in the src/main/java/PageController.java and add the following code:
package com.example;   
import  org.springframework.stereotype.Controller;  
import  org.springframework.web.bind.annotation.RequestMapping;
@Controller 
public  class   PageController  {  
   
@RequestMapping("/hello")                  
   
public  String  index(){                                   
       
return  "hello";                   
   
}   
}


Posted By Novida1:54 PM

Installation and Create Simple Spring Boot Project on Eclipse

Filled under:

Assalamualaykum wr. wb.
Konbanwa, minna san!
Kali ini saya ingin berbagi tutorial untuk membuat aplikasi dengan menggunakan Spring Boot yang di run pada IDE Eclipse. Sebenarnya tutorial ini terinspirasi penuh dari mata kuliah saya di Semester 2 di salah satu universitas negeri di Depok :D, Pemrograman Skala Perusahaan (Enterprise Programming) yang diajar langsung oleh Bapak Samuel Louvan. Kalau boleh cerita sedikit, saya sangat suka sekali dengan dosen satu ini, karena selain pintar (ya jelas lah ya), beliau sangat cakap, wawasannya luas, dan pernah bekerja di luar negri yang memproduksi tidak hanya software, tapi juga hardware. Selain itu cara memberikan tutorial juga sangat sistematis, sehingga siapapun juga pasti bias menguasai Spring Boot dengan baik setelahnya.

SEE THIS TUTORIAL IN ENGLISH


Three Tired Web Layers

Anda pasti mungkin belum atau sudah tahu konsep pemrograman web dengan memanfaatkan 3 layer ini:

http://martinfowler.com/bliki/PresentationDomainDataLayering.html

  1. Presentation Layer, adalah layer yang akan melakukan http request dan merender halaman HTML
  2. Domain Layer, adalah layer yang digunakan untuk meletakkan semua logika bisnis (business logic) yang memiliki validasi dan kalkulasi
  3. Data Layer, adalah layer yang digunakan untuk mengelola data di dalam database atau remote services

Presentation Layer

Presentation layer bertanggung jawab untuk menampilkan informasi kepada user dan menginterpretasikan perintah yang diberikan oleh user, dan selanjutnya diserahkan kepada domain dan data layer. Presentation layer biasanya ditunjukkan dengan menggunakan Graphical User Interface yang dapat dipahami oleh manusia sebagai user.
Adapun ciri-ciri spesifik dari presentation layer adalah:
  • Melakukan penanganan interaksi yang dilakukan antara user dan aplikasi
  • Dalam bentuk sederhana dapat berupa command-line atau text-based menu system
  • Dalam era saat ini, bentuknya berupa GUI atau HTML-based browser UI

Data Layer

Data layer bertanggung jawab dalam berkomunikasi dengan layer-layer lain sesuai dengan tugas-tugasnya terhadap aplikasi.
Adapun ciri-ciri spesifik dari data layer adalah:
  • berkomunikasi dengan database engine yang digunakan bersamaan dengan aplikasi
  • berkomunikasi dengan aplikasi lain
  • berkomunikasi dengan system Messaging
  • berkomunikasi dengan Transaction Manager

Domain Layer

Domain layer bertanggung jawab dalam pekerjaan yang dibutuhkan oleh aplikasi agar memenuhi kebutuhan atau requirement.
Adapun ciri-ciri spesifik dari Domain layer adalah:
  • Biasanya dikenal dengan sebutan business logic
  • melakukan kalkulasi terhadap input dan menyimpannya ke data
  • memberikan validasi terhadap data yang dilempar oleh presentation layer
  • Menggambarkan bagaimana sebuah data diformasikan dan dikirim ke data layer

Biasanya tujuan dibuatnya domain layer di tengah-tengah layer adalah untuk benar-benar menyembunyikan data dari presentation layer, sehingga dimungkinkan user pada presentation layer tidak dapat mengakses langsung data yang disimpan.

Bagaimana untuk memisahkan layer-layer tersebut?

Hal itu sangat bergantung pada kompleksitas aplikasi yang akan kita kembangkan. Semakin kompleks, 3 layer tersebut biasanya dibagi lagi menjadi kelas-kelas. Dan semakin tinggi kompleksitasnya, kelas-kelas tersebut juga akan dibedakan ke dalam beberapa package juga.

Pastikan bahwa anda melakukan pemisahan terhadap layer, setidaknya pada tingkat subroutine!

Rules

Domain layer dan data layer seharusnya tidak bergantung terhadap presentation layer.
Itu artinya, tidak boleh ada pemanggilan subroutine untuk kode domain layer dan data layer kedalam presentation layer.

Contoh penerapan dari dari Three tired ini adalah Spring Boot Framework. Dan sebentar lagi kita akan belajar untuk membuat project, supaya anda paham bagaimana Three tired dapat diaplikasikan.

OK! Untuk dapat membuat program Spring Boot dengan menggunakan Eclipse, berikut ini tools yang harus dipersiapkan:
  • Download JDK (Java Development Kit) di sini
  • Set path JAVA_HOME seperti ini
  • Install Eclipse yang sudah terinstall Maven dengan menggunakan Eclipse versi Neon di sini
  • Install Spring Tool Suite (STS) disini, atau dengan melalui Help -> Market Place dan install melalui page tersebut

Setelah semua tools siap, silakan buka Eclipse anda dan ikuti langkah-langkah berikut ini:

Membuat Project baru

  • Buka Eclipse

  • Install STS melalui Eclipse Market Place

  • Setelah semua yang berkaitan dengan STS finish, anda akan diminta untuk restart Eclipse, maka silakan untuk melakukan restart Eclipse
  • Untuk membuka project Spring Boot, silakan pilih menu File -> New -> Other...
  • Silakan memilih Spring Starter Project yang berada di dalam folder Spring

  • Biarkan saja secara default Eclipse akan mengisi properti yang dibutukan

  • Pilih Next, dan kemudian pilih Web dependency

  • Pilih Finish

Mengubah Port

  • Secara default, server yang digunakan Spring Boot memiliki port 8080. Jika anda ingin mengubahnya, anda bisa pergi src/main/resource untuk mengakses application.properties. Kemudian tambahkan kode berikut:
server.port = 9090
  • Pada src/main/java buatlah file baru dan beri nama HelloWorldController.java, lebih baik lagi jika anda telah terlebih dahulu memiliki package
  • Pada HelloWorldController.java, tambahkan kode sebagai berikut:
package com.example; 

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController public class HelloWorldController { 
   
@RequestMapping("/") 
   
public String index ()  { 
       
return "Hello World!"; 
   
} }


  • Jalankan project, Run As Java Application



Menggunakan Page Controller

  • Ubah pom.xml Tambahkan kode berikut ini:

<dependency>   
   
<groupId>org.springframework.boot</groupId>
   
<artifactId>spring-boot-starter-thymeleaf</artifactId> 

</dependency>



  • Membuat halaman HTML pada src/main/resources/templates/hello.html

<!DOCTYPE   html>   <html   xmlns:th="http://www.thymeleaf.org">                   
   
<head>                                 
       
<title>First    Page</title>                   
   
</head>                
   
<body>                                 
       
<h1>Hello,  world!</h1>                
   
</body> </html>









  • Membuat halaman controller untuk halaman HTML pada src/main/java/PageController.java


package com.example;   
   
import  org.springframework.stereotype.Controller;  import  org.springframework.web.bind.annotation.RequestMapping;
   
@Controller public  class   PageController  {  
   
@RequestMapping("/hello")                  
   
public  String  index(){                                   
       
return  "hello";                   
   
}   }



Posted By Novida10:37 AM