Loading...

All Questions

10 questions
18
votes
1
answers
657 views
Spring Data JPA Repository returns null instead of empty list

I have a JPA Repository method that should return a List, but it returns null instead of an empty list when no results are found. ```java public interface UserRepository extends JpaRepository { L...

asked 2 days ago
29
votes
1
answers
951 views
How to write unit tests for Spring Boot controllers with WebMvcTest?

I want to write unit tests for my REST controllers in Spring Boot. I've heard about @WebMvcTest but I'm not sure how to use it properly. How do I: 1. Test controller methods in isolation 2. Mock serv...

1.2k
asked 3 days ago
45
votes
1
answers
1251 views
What is the difference between Spring and Spring Boot?

I am new to Java development and I keep hearing about Spring and Spring Boot. What exactly is the difference between them? I understand that Spring is a framework, but what does Spring Boot add on to...

asked 3 days ago
44
votes
1
answers
1681 views
Best way to handle exceptions globally in Spring Boot REST API?

I want to handle all exceptions in one place in my Spring Boot application instead of using try-catch in every controller method. What's the best practice for global exception handling that returns p...

asked 4 days ago
32
votes
1
answers
891 views
How do I enable and secure Spring Boot Actuator endpoints?

I want to use Spring Boot Actuator to monitor my application in production. I added the dependency but all endpoints are exposed. How do I: 1. Enable only specific endpoints (health, info, metrics) 2...

850
asked 4 days ago
41
votes
1
answers
1121 views
How to validate request body in Spring Boot REST API?

I'm building a REST API and need to validate the request body. I have a UserDTO class: ```java public class UserDTO { private String email; private String name; private Integer age; } ```...

asked 5 days ago
27
votes
1
answers
781 views
How to use Spring Boot profiles for different environments?

I need to configure different database settings for dev, test, and production environments in my Spring Boot application. What's the best way to use profiles? Should I use separate YAML files or prof...

1.2k
asked 6 days ago
56
votes
1
answers
1802 views
LazyInitializationException in Spring Boot JPA - How to fix?

I'm getting this error when trying to access a collection after fetching an entity: ``` org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.example.User.p...

asked 6 days ago
52
votes
1
answers
1451 views
How to enable CORS in Spring Boot for my React frontend?

I have a Spring Boot backend running on localhost:8080 and a React frontend on localhost:3000. When I try to make API calls from React, I get: ``` Access to XMLHttpRequest has been blocked by CORS po...

asked 1 week ago
67
votes
1
answers
2102 views
@Autowired vs Constructor Injection - which is better in Spring Boot?

I've seen two ways to inject dependencies in Spring Boot: ```java // Field Injection @Autowired private UserService userService; // Constructor Injection private final UserService userService; publ...

850
asked 1 week ago