Barclays 2026 Java Spring Boot Microservices Interview Q&A: Deep Dive into Core Concepts
This article dissects a comprehensive set of Barclays Java Spring Boot microservices interview questions, covering HashMap internals, REST method semantics, code implementations, Spring dependency injection, JDBC evolution, JPA/Hibernate usage, SQL injection defenses, microservice architecture essentials, and best‑practice REST API design.
HashMap internal mechanism
HashMap implements the Map interface using a Node[] bucket array. The default initial capacity is 16 and is always a power of two. It allows one null key and multiple null values and does not guarantee element order. Each bucket may be a linked list or a red‑black tree. Core steps are computing the hash, locating the bucket index, handling collisions, resizing, and thread‑safety considerations. In Java 21 the implementation is unchanged; ZGC and Shenandoah improve performance for large maps. For ordered requirements use LinkedHashMap or TreeMap.
PUT vs POST vs PATCH semantics
POST creates a resource (non‑idempotent). PUT replaces a resource entirely (idempotent). PATCH performs partial updates and is the recommended approach for modern APIs. PUT is appropriate for full‑replace scenarios.
Hand‑written string array reversal
public class ReverseArrayWithoutPredefinedMethod {
public static void main(String[] args) {
String[] array = {"apple", "banana", "orange", "grape"};
System.out.println("Original array:");
printArray(array);
reverseArray(array);
System.out.println("
Reversed array:");
printArray(array);
}
public static void reverseArray(String[] arr) {
int left = 0;
int right = arr.length - 1;
while (left < right) {
String temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
public static void printArray(String[] arr) {
for (String s : arr) {
System.out.print(s + " ");
}
System.out.println();
}
}Time complexity O(n), space complexity O(1). Interview extensions may ask to reverse a char[].
Comparable vs Comparator
// Natural ordering
public class Employee implements Comparable<Employee> {
private String name;
private int salary;
@Override
public int compareTo(Employee o) {
return Integer.compare(this.salary, o.salary);
}
}
// Custom ordering (lambda + method reference)
employees.sort(Comparator.comparing(Employee::getName)
.thenComparing(Employee::getSalary));Prefer lambda expressions and method references; avoid anonymous inner classes.
JdbcTemplate to JdbcClient evolution (Spring 6.1+)
@Repository
public class ProductRepository {
private final JdbcTemplate jdbcTemplate;
public ProductRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public List<Product> findByCategory(String category) {
return jdbcTemplate.query(
"SELECT id, name, price FROM products WHERE category = ?",
(rs, rowNum) -> new Product(
rs.getLong("id"),
rs.getString("name"),
rs.getBigDecimal("price")
),
category
);
}
}For routine business use Spring Data JPA; for complex SQL use JdbcTemplate or the newer JdbcClient, which offers a more concise API.
Spring dependency injection best practice
@Service
public class OrderService {
private final OrderRepository orderRepository;
private final PaymentService paymentService;
public OrderService(OrderRepository orderRepository,
PaymentService paymentService) {
this.orderRepository = orderRepository;
this.paymentService = paymentService;
}
}Constructor injection is preferred; a single‑constructor class does not require @Autowired, improving testability.
SQL injection
// Vulnerable concatenation
String sql = "SELECT * FROM users WHERE username = '" + input + "'";
// Safe prepared statement
PreparedStatement ps = conn.prepareStatement(
"SELECT * FROM users WHERE username = ?"
);
ps.setString(1, input);Defensive strategies: use parameterized queries, ORM frameworks, and the principle of least privilege.
JPA / Hibernate core
String hql = "FROM Employee e WHERE e.department.name = :deptName"; public interface EmployeeRepository extends JpaRepository<Employee, Long> {
List<Employee> findByDepartmentName(String deptName);
}JPQL is preferred; method‑name‑derived queries are becoming mainstream.
Spring MVC request flow (microservice context)
In microservice scenarios responses are almost always JSON; the ViewResolver is rarely used.
Lazy vs eager loading (JPA)
@OneToMany(fetch = FetchType.LAZY)
private List<OrderItem> items;Default to LAZY fetching; use JOIN FETCH only when necessary.
Exception handling pitfall
Throwing an exception inside a finally block masks the original exception. Use try‑with‑resources to close resources automatically.
try (var conn = dataSource.getConnection()) {
// use connection
}Immutable object design
public final class Money {
private final String currency;
private final BigDecimal amount;
public Money(String currency, BigDecimal amount) {
this.currency = currency;
this.amount = amount;
}
}Java 17+ record alternative:
public record Money(String currency, BigDecimal amount) {}Microservice architecture core concepts (2026)
API Gateway
Service registration & discovery
Circuit breaker (Resilience4j)
Distributed transaction (Saga)
Observability (Micrometer + OpenTelemetry)
Suggested project layout:
/opt/projects/microservices
├── api-gateway
├── user-service
├── order-service
├── payment-servicePackage naming example:
com.icoderoad.orderservice
com.icoderoad.paymentEnterprise‑level REST API design
@RestController
@RequestMapping("/api/products")
public class ProductController {
@GetMapping("/{id}")
public Product get(@PathVariable Long id) { return null; }
@PostMapping
public Product create(@RequestBody ProductRequest req) { return null; }
@PatchMapping("/{id}")
public Product update(@PathVariable Long id,
@RequestBody Map<String,Object> updates) { return null; }
}Key points: resource‑oriented, stateless, JSON standardization.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
LuTiao Programming
LuTiao Programming is a friendly community offering free programming lessons. We inspire learners to explore new ideas and technologies and quickly acquire job-ready skills.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
