Home > Java Programming > Cara Get Data menggunakan Spring Boots

Cara Get Data menggunakan Spring Boots

September 1, 2023 Leave a comment Go to comments

Java Spring boot adalah tool untuk pembuatan web aplikasi dan microservices dengan menggunakan spring framework.

Tapi artikel ini hanya menjelaskan bagaimana spring boot melakukan get data.

Petama buat terlebih dahulu project spring boot. (lihat cara pembuatan project disini).

setelah pembuatan project spring boot, lalu buat 3 package.

  1. Package pertama untuk menempatkan class controller.
  2. Package kedua untuk menempatkan class service.
  3. Package keempat untuk menempatkan class response.

Package diatas adalah package yang biasa dibuat oleh admin, jadi package tersebut bisa saja berbeda sesuai dengan pembuat project tersebut.

Lalu buat class controller dengan nama ProfileController, dimana class ini akan diketahui sebagai class controller dari data profile.

@RestController
public class ProfileController {
	
	
}

Lalu gunakan annotation RestController sehingga class ini akan dianggap sebagai restController.

Sebelum pembuatan method didalam class ProfileController. buat terlebih dahulu class ProfileService didalam package service dan buat class response didalam package response.

Buat class Response seperti dibawah ini :

package com.test.mandiri.legacy.dto.response;
import lombok.Data;

@Data
public class ResponseProfile {
		private Integer profileCode;
		private String wantedJobTitle;
		private String firstName;
		private String lastName;
		private String email;
		private String phone;
		private String country;
		private String city;
		private String address;
		private Integer postcode;
		private String drivingLicense;
		private String nationality;
		private String placeOfBirth;
		private String dateOfBirth;
		private String photoUrl;
	
}

Class ResponseProfile ini menggunakan lib lombok untuk memudahkan pembuatannya (yaitu tidak perlu dideklarasikan lagi untuk getter dan setter-nya). Class ResponseProfile berfungsi untuk pengiriman response API dari get data profile.

Yang terakhir buat class ProfileService, class ini akan berfungsi untuk melakukan logic data dan penarikan data.

package com.test.mandiri.legacy.service;

import org.springframework.stereotype.Service;
import com.test.mandiri.legacy.dto.response.ResponseProfile;

@Service
public class ProfileService {

	public ResponseProfile getDataProfile(Integer idProfile) {
		ResponseProfile responseProfile = new ResponseProfile();
		
		if (idProfile != 0l && idProfile == 12345678) {
				
				responseProfile.setProfileCode(12345678);
				responseProfile.setWantedJobTitle("Software Engineer");
				responseProfile.setFirstName("Namaku");
				responseProfile.setLastName("Ukaman");
				responseProfile.setEmail("ukaman.namaku@gmail.com");
				responseProfile.setPhone("0812092039930");
				responseProfile.setCountry("Indonesia");
				responseProfile.setCity("Jakarta");
				responseProfile.setAddress("Jl.Gatot Subroto");
				responseProfile.setPostcode(20001);
				responseProfile.setDrivingLicense("123094995900");
				responseProfile.setNationality("Indonesia");
				responseProfile.setPlaceOfBirth("Maluku");
				responseProfile.setDateOfBirth("07-12-1988");
				responseProfile.setPhotoUrl("/app/upload/photo/1234595.png");
		} 
		return responseProfile;
	}
}

Karena ini class service maka gunakan annotation @service sebagai penanda bahwa class ini adalah service dan Class service ini akan dipanggil/include ke dalam class ProfileController.

Class service ini juga menggunakan class ResponseProfile menjadi object untuk mengirimkan data dummy yang akan dikirimkan ke response API profile.

Setelah selesai dengan proses pembuatan class ProfileService dan ResponseProfile, lalu kembali ke class ProfileController untuk pembuatan method-nya.

di-class ProfileController tambahkan method dengan nama getProfile dengan hasil return ResponseProfile.

tambahkan annotation @GetMapping pada method tersebut sebagai penggenal bahwa method ini memiliki fungsi get data dan didalam annotation GetMapping masukan endpoint url API untuk pemanggilan API-nya.

Lalu masukan juga annotation @PathVariable didalam mehtod tersebut karena endpoint dari API menggunakan variable didalamnya maka dibutuhkan untuk menggunakan annotation @PathVariable. Annotation @PathVarible ini berfungsi menunjukkan bahwa parameter metode harus diikat ke variabel template URI. Didukung untuk metode pengendali beranotasi RequestMapping.

Lalu gunakan juga annotation @Autowired dimana class ProfileController ini akan memanggil class ProfileService atau injection class ProfileService.

Contoh class seperti dibawah ini :

package com.test.mandiri.legacy.controller;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.test.mandiri.legacy.dto.response.ResponseProfile;
import com.test.mandiri.legacy.service.ProfileService;

@RestController
public class ProfileController {
	
	@Autowired
	private ProfileService profileService;
	
	@GetMapping("/api/profile/{idProfile}")
	public ResponseProfile getProfile(@PathVariable(required = true, name = "idProfile") Integer idProfile) {
		return profileService.getDataProfile(idProfile);
		
	}
}

Setelah pembuatan class ini saat untuk lakukan test dengan menggunakan tool Postman aplikasi.

buka aplikasi Postman lalu buat satu request dengan nama Get Profile, pilih metode dengan Get karena akan melakukan test pada API request get. lalu isi url API yang akan ditest. Lalu pilih tab body dan pilih raw dengan format JSON. setelah itu klik tombol send untuk lakukan test.

Contoh API ini berjalan dilokal server dengan port 8080 dan end point /api/profile/{idProfile}

Categories: Java Programming
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment