⚙️ Backend
Spring Boot Framework
Last updated: 2025-09-25 02:29:54
Spring Boot Java Framework
Spring Boot makes it easy to create stand-alone, production-grade Spring-based applications.
Basic Spring Boot Application
// Main Application Class
@SpringBootApplication
public class MyAppApplication {
public static void main(String[] args) {
SpringApplication.run(MyAppApplication.class, args);
}
}
// REST Controller
@RestController
@RequestMapping("/api/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public List getAllUsers() {
return userService.findAll();
}
@GetMapping("/{id}")
public ResponseEntity getUserById(@PathVariable Long id) {
Optional user = userService.findById(id);
return user.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
@PostMapping
public ResponseEntity createUser(@Valid @RequestBody User user) {
User savedUser = userService.save(user);
return ResponseEntity.status(HttpStatus.CREATED).body(savedUser);
}
@PutMapping("/{id}")
public ResponseEntity updateUser(@PathVariable Long id, @Valid @RequestBody User user) {
if (!userService.existsById(id)) {
return ResponseEntity.notFound().build();
}
user.setId(id);
User updatedUser = userService.save(user);
return ResponseEntity.ok(updatedUser);
}
@DeleteMapping("/{id}")
public ResponseEntity deleteUser(@PathVariable Long id) {
if (!userService.existsById(id)) {
return ResponseEntity.notFound().build();
}
userService.deleteById(id);
return ResponseEntity.noContent().build();
}
}
JPA Entity and Repository
// Entity
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 100)
@NotBlank(message = "Name is required")
private String name;
@Column(nullable = false, unique = true)
@Email(message = "Invalid email format")
private String email;
@Column(name = "created_at")
@CreationTimestamp
private LocalDateTime createdAt;
@Column(name = "updated_at")
@UpdateTimestamp
private LocalDateTime updatedAt;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List posts = new ArrayList<>();
// Constructors, getters, setters
public User() {}
public User(String name, String email) {
this.name = name;
this.email = email;
}
// ... getters and setters
}
// Repository
@Repository
public interface UserRepository extends JpaRepository {
Optional findByEmail(String email);
List findByNameContainingIgnoreCase(String name);
@Query("SELECT u FROM User u WHERE u.createdAt > :date")
List findUsersCreatedAfter(@Param("date") LocalDateTime date);
@Modifying
@Query("UPDATE User u SET u.name = :name WHERE u.id = :id")
int updateUserName(@Param("id") Long id, @Param("name") String name);
}
// Service
@Service
@Transactional
public class UserService {
@Autowired
private UserRepository userRepository;
public List findAll() {
return userRepository.findAll();
}
public Optional findById(Long id) {
return userRepository.findById(id);
}
public Optional findByEmail(String email) {
return userRepository.findByEmail(email);
}
public User save(User user) {
return userRepository.save(user);
}
public boolean existsById(Long id) {
return userRepository.existsById(id);
}
public void deleteById(Long id) {
userRepository.deleteById(id);
}
public List searchByName(String name) {
return userRepository.findByNameContainingIgnoreCase(name);
}
}
Configuration and Security
// Application Properties
# application.yml
spring:
datasource:
url: jdbc:postgresql://localhost:5432/myapp
username: ${DB_USERNAME:admin}
password: ${DB_PASSWORD:password}
driver-class-name: org.postgresql.Driver
jpa:
hibernate:
ddl-auto: validate
show-sql: false
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
format_sql: true
security:
oauth2:
client:
registration:
google:
client-id: ${GOOGLE_CLIENT_ID}
client-secret: ${GOOGLE_CLIENT_SECRET}
logging:
level:
com.myapp: DEBUG
org.springframework.security: DEBUG
server:
port: 8080
// Security Configuration
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Autowired
private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
@Autowired
private JwtRequestFilter jwtRequestFilter;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
return config.getAuthenticationManager();
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeHttpRequests(authz -> authz
.requestMatchers("/api/auth/**").permitAll()
.requestMatchers("/api/public/**").permitAll()
.requestMatchers(HttpMethod.GET, "/api/users").hasAnyRole("USER", "ADMIN")
.requestMatchers(HttpMethod.POST, "/api/users").hasRole("ADMIN")
.requestMatchers("/api/admin/**").hasRole("ADMIN")
.anyRequest().authenticated()
)
.exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint)
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
return http.build();
}
}