← Back to list

spring: rest api

benim için önemli notlardır.

Berkay Derin · 2025-01-22 13:10 · 1 claps · 1.4 min read
Open on Medium ↗

spring: rest api

benim için önemli notlardır.

http status code 1xx => information 2xx => success 3xx => redirection 4xx => client error 5xx => server error

katmanlı mimari nedir?

katmanlı mimari nedir?

controller: dışarıdan gelen isteği karşılayan katmandır.

service: logiclerin bulunduğu kısımdır. iş yükü buradadır. filtrelemeler vs. burada yapılır.

repository: veritabanına erişmek için kullandığımız katmandır.

anotasyonlar

@RestController : bir sınıfın üzerine yazdığımızda controller katmanı olur ve karşılanır. @Service : katmanın servis olduğunu belirtmek için. @Repository : katmanın repository olduğunu belirtmek için.

yukarıdaki anotasyanları kullandığımızda spring context’te üçünden de bean oluşur.

@AutoWired: enjekte etmek demektir. Bir tane sınıfı anotasyonla işaretlediğimizde 3 sınıftanda bean oluşturulur. Kullanmak istediğimizde annotation config applicationcontext sınıfı üzerinden erişebiliyoruz. Artık context’e gerek yok. @AutowWired ile bean otomatik oluşturuyoruz. Katmanlar arasında otomatik geçiş yapıyoruz.

IoC (Inversion of Control) => spring container veya spring context’te (artık ne dersen de) bean oluşturduğumuzda ve bu yönetimi spring’e devredersek IoC denir.

DI (dependency injection) => bean’i alıp herhangi bir class içerisine enjekte etmeye DI denir.

@PathVariable : dışarıdan gelen id değerini bana getir demek için kullanırız. @RequestParam : “?key=value&key=value” filtrelemede kullanılır. @RequestBody : HTTP isteklerinin gövdesindeki (body) verileri Java nesnelerine otomatik olarak dönüştürmek için kullanılır.

RestEmployeeController.java :

@RestController
@RequestMapping("/api/employee")
public class RestEmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping(path = "/list")
    public List<Employee> getAllEmployeeList() {
        return employeeService.getAllEmployeeList();
    }

    @GetMapping(path = "/list/{id}")
    public Employee getEmployeeById(@PathVariable(name = "id", required = true) String id) {
        return employeeService.getEmployeeById(id);
    }

    @GetMapping(path = "/with-params")
    public List<Employee> getEmployeeWithParams(
            @RequestParam(name = "firstName", required = false) String firstName, // required = false ise parametre
                                                                                  // gönderilmeyebilir.
            @RequestParam(name = "lastName", required = false) String lastName) {

        return employeeService.getEmployeeWithParams(firstName, lastName);
    }

    @PostMapping(path = "/add")
    public Employee addEmployee(@RequestBody Employee newEmployee) {
        return employeeService.addEmployee(newEmployee);
    }

    @DeleteMapping(path = "/delete/{id}")
    public boolean deleteEmployee(@PathVariable(name = "id", required = true) String id) {
        return employeeService.deleteEmployee(id);
    }

    @PutMapping(path = "/update/{id}")
    public Employee updateEmployee(@PathVariable(name = "id", required = true) String id,
            @RequestBody UpdateEmployeeRequest req) {
        return null;
    }
}

메타데이터
post_id
2e174c8394d5
slug
spring-rest-api-2e174c8394d5
url
https://medium.com/@Zewodi/spring-rest-api-2e174c8394d5
canonical_url
https://medium.com/@Zewodi/spring-rest-api-2e174c8394d5
author_url
https://medium.com/@Zewodi
status
ok
fetched_at
2026-07-21 05:58:31