Skip to content

blg-kadr/java-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

Comprehensive Java Backend Engineering Cheatsheet

1. Basic Java

Variables

int number = 10;
String message = "Hello, World!";

Control Flow

if (number > 5) {
    System.out.println("Number is greater than 5");
} else {
    System.out.println("Number is 5 or less");
}

OOP

class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Bark");
    }
}

2. Spring Boot Fundamentals

Dependency Injection

@Service
public class MyService {
    @Autowired
    private MyRepository myRepository;
}

Configuration

# application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=pass

Profiles

@Profile("dev")
@Component
public class DevConfig {...}

3. REST API Development

Controller Examples

@RestController
@RequestMapping("/api")
public class MyController {
    @GetMapping("/items/{id}")
    public ResponseEntity<Item> getItem(@PathVariable Long id) {
        return ResponseEntity.ok(itemService.findById(id));
    }

    @PostMapping("/items")
    public ResponseEntity<Item> createItem(@RequestBody Item item) {
        return ResponseEntity.created(...).body(itemService.save(item));
    }
}

4. Spring Data JPA

Entity Definitions

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;

    @OneToMany(mappedBy = "user")
    private List<Order> orders;
}

Repository Patterns

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    List<User> findByName(String name);
}

5. DTOs and Mapping

DTO Pattern

public class UserDTO {
    private Long id;
    private String name;
}

Manual Mapping

User user = userRepository.findById(id);
UserDTO userDTO = new UserDTO();
userDTO.setId(user.getId());
userDTO.setName(user.getName());

Using MapStruct

@Mapper
public interface UserMapper {
    UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);
    UserDTO userToUserDTO(User user);
}

6. Validation

Annotations

public class User {
    @NotNull
    private String name;

    @Email
    private String email;
}

7. Exception Handling

Global Error Handling

@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(CustomException.class)
    public ResponseEntity<String> handleCustomException(CustomException ex) {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
    }
}

8. Collections and Streams

List Example

List<String> names = Arrays.asList("Alice", "Bob", "Charlie");

Stream API

List<String> filtered = names.stream()
    .filter(name -> name.startsWith("A"))
    .collect(Collectors.toList());

9. Spring Security

Security Configuration

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .anyRequest().authenticated()
            .and().httpBasic();
    }
}

JWT Authentication

public class JwtTokenUtil {...}

10. Testing

JUnit 5

@SpringBootTest
public class MyServiceTests {...}

Mockito Example

@Mock
private MyRepository myRepository;

11. Advanced Patterns

Builder Pattern

class User {
    private final String name;
    private final int age;

    public static class Builder {
        private String name;
        private int age;

        public Builder name(String name) {
            this.name = name;
            return this;
        }

        public Builder age(int age) {
            this.age = age;
            return this;
        }

        public User build() {
            return new User(this);
        }
    }
}

12. Logging

SLF4J Examples

private static final Logger logger = LoggerFactory.getLogger(MyClass.class);
logger.info("Log message");

13. Pagination and Sorting

Using Pageable

Pageable pageable = PageRequest.of(0, 10);
Page<User> users = userRepository.findAll(pageable);

14. Async Processing

@Async
public CompletableFuture<String> asyncMethod() {...}

15. Caching

@Cacheable("users")
public User getUser(Long id) {...}

16. Best Practices

  • Follow SOLID principles.
  • Keep code clean and maintainable.
  • Use meaningful variable names.

This cheatsheet provides a quick reference for essential Java backend engineering concepts and practices, ensuring developers can build robust applications with Spring Boot and related technologies effectively.

Some useful references https://introcs.cs.princeton.edu/java/11cheatsheet/?utm_source=chatgpt.com @blg-kadr/Java-Cheatsheet-fork https://cheatography.com/lucamazzza/cheat-sheets/java17/pdf/?last=1696235600 https://www.baeldung.com/java-collections?utm_source=chatgpt.com

About

Comprehensive Java Programming Cheatsheet

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages