Posts

Showing posts from June, 2019

Service In Spring Boot For Beginners

Image
Service in spring boot is component where you usually place your business logic of your application. So service will be the class where mostly you will write Java code. Business logic can be anything for example, when user update profile, buying items, checkout, etc.

So here's an example of simple service in spring for user registration, login, update, and remove.

@Service
public class UserService{

@UserRepository
private UserRepository userRepository;

public User registration(User newUser){
return userRepository.save(newUser);
}

public User login(UserDto user){
return userRepository.findByUsername(user.getUsername());
}

public User update(Long id, User newUser){
User user = userRepository.findById(id).orElse(new User);
user.setUsername(newUser.getUsername());
user.setName(newUser.getName());
userRepository.save(user);
return user;
}

public void remove(Long id){
userRepository.deleteById(id);
}
}

To make a class as a service, you add a @Service annotation, and on the above code we are auto wiring Use…

Repository in Spring Framework For Beginners

Image
Repository in spring is classes use to interact with database, that is simplify as CRUD (create, read, update and delete). To make a class repository, you add a @Repository annotation to the interfaces, but you also need to extends one of spring repository class that is CrudRepository, JpaRepository or PagingAndSortingRepository. And you need to mention your DAO, for example.

@Repository
public interface UserRepository extends JpaRepository<UserDao, Long> {
}

@Entity
class UserDao{
private Long id;
private String name;
}

Actually that's enough, now you can use your repository anywhere you want, just inject that component, for example to find a user you can use it.


@Controller
@RequestMapping("/user")
public Class UserController{
@Autowired 
private UserRepository userRepository;

@GetMapping("findAll")
public List<UserDao> allUser(){
return userRepository.findAll();
}
}

The above class is an example on how to get all user expose by endpoint "user/findAll".

Model In Spring Framework For Beginners

Image
Model in programming language is a representative of an object, so if you have a car object, you can define a car model, for example, car brand, car speed, car price, etc. In pseudo code you can create a car model, something like.

Class Car{
String brand;
Integer speed;
Float price;
}

That's called a model. Now in spring framework, you can add some annotation to your model, so that it can be integratted to you database, those model usually will be call as an entity, so the first thing is to set annotation to car class an @Entity annotation.

@Entity
Class car{
String brand;
Integer speed;
Float price;
}

But, that class should have at least an id it is recommended to have an id to whatever model you want create, so you must have an id field.

@Entitypublic class Car{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id;private String brand;private Integer speed;private Float price;}
Now, your entity is ready, but it useless since it doesn't have any public fiel…

Controllers In Spring Framework For Java Beginners

Image
It is really hard to build web application if you are just a beginner, but if you spare your time seriously learning web development, you're eventually gonna make it. Spring is a Java framework for web development, by using framework, you don't to focus on some core stuff like http stuff, security, database connection, all you gonna need to do is just some little configuration and more focus on writing business logic for your application.

If you've already finish the first part and successfully running your application on the browser, the next step is you just need to learn more, to read more stuff about spring framework. This part will be just about controller. Controller is to where you decide an endpoint, usually on web application, user will request a page or a service, we called it end point.


1. Simple hello world controller

@Controller
@RequestMapping("hello")
public class HelloController{

@GetMapping
public String hello(){
return "Hello World!";
}
}


@Cont…

Web Development Using Spring Boot For Beginners

Image
In this series of posts, we are gonna cover tutorial on how to get start web development in Java using spring boot. For your information, I've been working on web developments for quite a while but not using Spring, and not using Java either, so it's kinda like i am also newbie as you are, but don't worry all codes on this website will be tested so that it will work when you copy/pasting. And because it's for beginners, all codes on this series would be full single class file, no cut.

Follow these steps to get started web development in Spring.

1. Prepare your favorite IDE
It is Java not Javascript, you are gonna write enterprise level codes, in other to have readable, maintainable codes, you need to have better tools, IDE helps you refactoring your code, code completion, and building your code with a press of a button, usually hammer icon.

2. Initialize spring boot project
Simply using start.spring.io, you can create your project template, all you need to do is specify…