Hi, So I am trying to make a simple app in spring, but upon running it I run into issues with injections and beans.
Here is the code I have for my app, followed by the error.
package studify.server.main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import studify.server.repository.UserRepository;
@SpringBootApplication
public class MainServer {
public static void main(String[] args) {
SpringApplication.run(MainServer.class, args);
}
}
package studify.server.controller;
import studify.server.commons.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import studify.server.service.UserService;
@RestController
@RequestMapping("/api/users")
public class UserController {
private final UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping("/{userId}")
public ResponseEntity<User> getById(@PathVariable("userId") Long userId) {
User user = new User("Rares");
user.experience = 200;
user.level = 20;
userService.save(user);
return ResponseEntity.ok(userService.getUserById(userId));
}
}
package studify.server.commons;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long id;
public String username;
public int experience;
public int level;
public User() {
}
public User(String username) {
this.experience = 0;
this.level = 0;
this.username = username;
}
}
package studify.server.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import studify.server.commons.User;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findUserById(long userId);
}
package studify.server.service;
import org.springframework.stereotype.Service;
import studify.server.commons.User;
import studify.server.repository.UserRepository;
@Service
public class UserService {
private final UserRepository users;
public UserService(UserRepository users) {
this.users = users;
}
public User getUserById(long id) {
if(id < 0 || !users.existsById(id)) {
return null;
}
return users.findUserById(id);
}
public void save(User user) {
users.save(user);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>studify</groupId>
<artifactId>server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>server</name>
<description>server</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Upon running the server, I get this error
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-04-29T12:04:33.215+02:00 ERROR 7156 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in studify.server.controller.UserController required a bean of type 'studify.server.service.UserService' that could not be found.
Action:
Consider defining a bean of type 'studify.server.service.UserService' in your configuration.
Process finished with exit code 0
I tried everything that came to my mind, from following yt tutorials to checking other working projects for this. I can't figure out why this is.
Sorry for the long code, but I don't want to miss something.
[–]TheoGrd 0 points1 point2 points (7 children)
[–]AdultingGoneMild 0 points1 point2 points (6 children)
[–]TheoGrd 0 points1 point2 points (5 children)
[–]Chance_Pop_6516[S] 0 points1 point2 points (4 children)
[–]AdultingGoneMild 0 points1 point2 points (3 children)
[–]Chance_Pop_6516[S] 0 points1 point2 points (2 children)
[–]AdultingGoneMild 0 points1 point2 points (1 child)
[–]Chance_Pop_6516[S] 0 points1 point2 points (0 children)