The First Month & Introduction to Spring Boot 3

How to Develop and Survive in a Big Company & Key concepts of Spring Framework: An Elementary Level API

Baysan
Nerd For Tech
8 min readAug 5, 2023

--

I started to get used to work in a corporate culture. To be honest it became easier than I thought earlier. This story mainly will be 3 chapters:

  • How to Develop in a Big Company: How I do development, what mostly I am doing while coding etc.
  • How to Survive in a Big Company: Management, How we organize the tasks, what the management expect from us to do, and importance of KPIs.
  • Introduction to Spring Boot 3

Also, the text is going to be a summary. I was thinking write in depth, then I just decided to write summary.

Photo by Nubelson Fernandes on Unsplash

I’ve written about my first two weeks at the company in the story below.

How to Develop in a Big Company

I know this is a weird question like “how to read book?” 🥲 But I think there are some points that we should point out.

Nothing is different in here too. The roadmap is the same as usual what we do.

  • Cost of the requirements & Planning
  • Defining & Assigning the task
  • Developing, Testing, Deploying

For me, one of the best sides is that the task is already planned when it is hand off to me. I know what will be the problem that I am going to solve. It helps me to figure out the components that I’ll use/interact with it. But isn’t it the norm that we face in everywhere? Actually, no. When I was working alone or with a small team, I firstly was trying to understand what the customer needs to do. Obviously, working with a business analyst makes faster and easy to work on the problem.

Despite this benefit, one of the worst side is that I generally ask too much about the projects like what data comes how or what is the component that I need to modify etc. It makes me feel that I am too slow. But it is the nature of the situation, they also say that too, on-boarding.

Once we developed the feature, it goes to be approved by business department (we could say UAT). And if it is approved, it is ready to go to production. If it is not approved, it returns to the developer to fix the bugs. I assume that the code passed unit tests, integration tests etc. 🥲

How to Survive in a Big Company

In this section, KPIs and OKRs are coming into the scene. In my company, a year is divided into 2 parts as first and second half. We define/set KPIs and OKRs in the first half. Before we start the second half, the KPIs and OKRs are re-set/re-defined if needed. For an instance, I’d say the below what are possible OKRs for us.

  • A training program that sharpes our skills
  • A book that boosts us in a specific field or skillset
  • Being a part of an academic paper or work
  • Writing articles, stories etc. about the company (how we solved X by implementing Y etc.)
  • Do we quick respond to urgent/critical tasks?

I really loved this process because of it pushes teams to work(!). Also, It directly affect your yearly performance which is supporting you by increasing your seniority. Thus, no one is able to brush-off the tasks. In my work life, I have experience with teams that hadn’t complete tasks from 2–3 years ago, even hadn’t examine 🥲

Introduction to Spring Boot 3

I strongly suggest you to 2 books about Java and Spring Boot. Transitioning to Java really helped me to transition from Python to Java. And before we start to code, I am going to touch some points of key concepts of Spring Framework.

What Solves Spring Boot?

As of my readings, I understood that Spring Boot’s magic is applying Dependency Injection (Dependency Inversion principle) in best. When a Spring application starts up, it creates a container of sorts and registers Java Beans to the Spring application context. Java Beans in Spring context are called Spring Beans.

Application Context | Image from baeldung.com

When Spring Framework found a need of a bean, follows the steps below.

  • businessBean (object) needs a HelperBean.
  • Ask the application context for a HelperBean.
  • If the application context has the HelperBean returns it, else creates a HelperBean and returns it.
  • businessBean (object) does it job by using the application context’s HelperBean.
  • BusinessBean (type, class) is registered in the application context under the name businessBean (object).

What Java Bean (or Spring Bean) is

Actually, it is just an object. But an object follows a specific pattern.

  • Java Bean’s all the fields are private.
  • Java Bean provides access to its fields through getters and setters .
  • Java Bean has a no-argument constructor.
  • Java Bean implement the Serializable interface (not enforced).

Dependency Injection Into a Class

There are 3 ways to inject dependencies into classes with constructor methods in Spring Framework. Spring finds the needed dependencies in the application context, and inject them into where they are neded for us. This is known as Autowiring. Component Scanning help us to autowire the dependencies.

  • @Autowired: Spring Framework annotation marks methods to inject dependencies. This annotation can be applied to constructors, setter methods and fields. Private fields too. If class has only one constructor, there is no need to apply @Autowired annotation. Spring assumes it will be autowired.
  • @Component: The class itself can be marked with one of Spring’s @Component annotations: @Service, @Controller, @RestController or @Configuration or just @Component.

An Elementary API

In this part of the article, we are going to code a simple Todo API. I am going to use VS Code and the following extensions.

Spring Framework Extensions
Java Extensions

By using Spring Initializr, you can easily create a Spring project by executing the command: Spring Initializr: Create a Maven Project. I selected the dependencies below.

Elementary API Dependencies

You also can use the link below to generate a Spring project and then open it up in your favorite IDE/Editor.

Todo Record

I’ll use Record class to map my entities. It has 3 fields.

package com.mebaysan.elementaryapi;

public record Todo(Long ID, String todoTitle, String totoDescription) {}

Basic Controller

Now I need to create endpoints. @RestController helps us to create our controller and serialize and deserialize the requests.

  • @<Method>Mapping annotations help us to map the requests by type.
  • @RequestBody annotation deserialize the request data to the type that is next to it.
  • @PathVariable help us to access the path parameters like “/12”. We can extract parameters and map them to variables such as the example below: id.
package com.mebaysan.elementaryapi;

import java.util.List;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PathVariable;


@RestController
public class TodoRestController {
private static final String URL = "/todo";

@GetMapping(URL)
public List<Todo> getTodos() {

return List.of();

}

@PostMapping(URL)
public Todo addTodo(@RequestBody Todo todo) {
return todo;
}

@DeleteMapping(URL + "/{id}")
public void deleteTodo(@PathVariable("id") Long id) {
// Delete todo
}

}

Todo Repository

We will use a static List as a repository. But binding real data sources is really simple in Spring Framework. You can check the link here to see an example.

Our Repository class below holds the Todos in an ArrayList. And it has main 2 methods, addTodo and getTodos. Whenever an instance of the class is created, it seeds itself to provide pre-built data.

package com.mebaysan.elementaryapi;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

@Service
public class TodoRepository {
private List<Todo> todos = new ArrayList<Todo>();

private void seed() {
addTodo(new Todo(1L, "Todo 1", "Todo 1 description"));
addTodo(new Todo(2L, "Todo 2", "Todo 2 description"));
addTodo(new Todo(3L, "Todo 3", "Todo 3 description"));
}

public void addTodo(Todo todo) {
todos.add(todo);
}

public List<Todo> getTodos() {
return todos;
}

public void deleteTodoById(Long id) throws Exception {
Todo todo = todos.stream().filter(t -> t.ID().equals(id)).findFirst().orElseThrow(() -> new Exception());
todos.remove(todo);
}

public TodoRepository() {
seed();

}

}

Dependency Injection

Now we are ready to see the Spring’s magic in action. We defined a field into the controller that will be injected: todoRepository. Because of we marked TodoRepository as @Service, Spring automatiacally inject it into the proper place by using Autowiring. Also, we chose constructor injection as injection method because of we created a constructor method with the needed bean parameter.

package com.mebaysan.elementaryapi;

import java.util.List;
import java.util.Optional;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TodoRestController {
private static final String URL = "/todo";
private TodoRepository todoRepository;

public TodoRestController(TodoRepository todoRepository) {
this.todoRepository = todoRepository;
}

@GetMapping(URL)
public List<Todo> getTodos() {

return todoRepository.getTodos();

}

@PostMapping(URL)
public ResponseRecord addTodo(@RequestBody Todo todo) {
return new ResponseRecord(true, "Todo added", Optional.of(todo));
}

@DeleteMapping(URL + "/{id}")
public ResponseRecord deleteTodo(@PathVariable("id") Long id) {
try {
todoRepository.deleteTodoById(id);
return new ResponseRecord(true, "Todo deleted", Optional.empty());
} catch (Exception e) {
String message = "Todo not found. Error: " + e.getMessage();
return new ResponseRecord(false, message, Optional.empty());
}

}

}
// to standardize the response messages: ResponseRecord.java

package com.mebaysan.elementaryapi;

import java.util.Optional;

public record ResponseRecord(boolean Status, String Message, Optional<Object> Data) {
}

Testing Our API

I am going to test by using Postman.

Get: /todo
Post: /todo
Delete: /todo/1 (success)
Delete: /todo/1 (error, we already removed it)

Finally

Hopefully, you enjoyed. To be honest, I know the code is not surrounded with best practices. But it is because of I am so new in Spring World. You can also use ResponseEntity from Spring Framework instead of our Response Record. I’ll keep try to do my best. You can find my links below to follow me on other platforms.

Kind regards

--

--

Baysan
Nerd For Tech

Lifelong learner & Developer. I use technology that helps me. mebaysan.com