Step-by-Step Guide: Creating a Java Server for Backend Web Development

Deepak Ranolia
2 min readDec 4, 2023

--

Step 1: Install Prerequisites

Ensure that you have the following installed on your system:

  • Java Development Kit (JDK)
  • Integrated Development Environment (IDE) such as IntelliJ IDEA or Eclipse
  • MongoDB (or any preferred database)

Step 2: Create a Spring Boot Project

  1. Open your IDE and create a new Spring Boot project.
  2. Choose a Group and Artifact name, and select the necessary dependencies (e.g., Spring Web, Spring Data MongoDB).
  3. Click “Finish” to generate the project.

Step 3: Define Entity Class

Create a User entity class representing the user profile. Annotate it with @Document for MongoDB.

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "users")
public class User {
@Id
private String id;
private String username;
private String email;
// Getters and setters
}

Step 4: Create Repository Interface

Create a UserRepository interface that extends MongoRepository for database operations.

import org.springframework.data.mongodb.repository.MongoRepository;

public interface UserRepository extends MongoRepository<User, String> {
// Custom queries if needed
}

Step 5: Implement Service Class

Create a service class to handle business logic. For simplicity, let’s create a CRUD service.

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

import java.util.List;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public List<User> getAllUsers() {
return userRepository.findAll();
}
public User getUserById(String id) {
return userRepository.findById(id).orElse(null);
}
public User createUser(User user) {
return userRepository.save(user);
}
public User updateUser(String id, User updatedUser) {
if (userRepository.existsById(id)) {
updatedUser.setId(id);
return userRepository.save(updatedUser);
}
return null;
}
public void deleteUser(String id) {
userRepository.deleteById(id);
}
}

Step 6: Create Controller

Implement a controller class to handle HTTP requests.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public List<User> getAllUsers() {
return userService.getAllUsers();
}
@GetMapping("/{id}")
public User getUserById(@PathVariable String id) {
return userService.getUserById(id);
}
@PostMapping
public User createUser(@RequestBody User user) {
return userService.createUser(user);
}
@PutMapping("/{id}")
public User updateUser(@PathVariable String id, @RequestBody User updatedUser) {
return userService.updateUser(id, updatedUser);
}
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable String id) {
userService.deleteUser(id);
}
}

Step 7: Configure MongoDB Connection

In the application.properties file, configure MongoDB connection properties.

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=mydatabase

Step 8: Run the Application

Run your Spring Boot application. You should see it starting up, and you can access the CRUD API at http://localhost:8080/api/users.

Conclusion

Congratulations! You’ve successfully set up a Java server for backend web development using Spring Boot and MongoDB. You now have a basic CRUD API for managing user profiles. Extend and customize this example based on your application requirements.

--

--

Deepak Ranolia
Deepak Ranolia

Written by Deepak Ranolia

Strong technical skills, such as Coding, Software Engineering, Product Management & Finance. Talk about finance, technology & life https://rb.gy/9tod91