gpt4 book ai didi

java - Springboot UnsatisfiedDependencyException?

转载 作者:太空宇宙 更新时间:2023-11-04 09:21:15 24 4
gpt4 key购买 nike

我正在使用 Spring boot 设置基本的 REST,但遇到一些我不知道如何修复的错误。

希望大家能帮帮我:)

我已经尝试了一些注释修复,但没有用...您可以在下面找到我的代码。也许依赖关系不正确?

用户 Controller :

package com.spring.timelybackend.controllers;

import com.spring.timelybackend.model.Auth;
import com.spring.timelybackend.model.User;
import com.spring.timelybackend.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/users")
public class UserController {

@Autowired
private UserService userService;

private Auth auth = new Auth();
private User user = new User();

@CrossOrigin(origins = "http://localhost:4200")
@RequestMapping(value = "/login", method = RequestMethod.POST)
public Auth login(@RequestBody User _user) {
try {
auth = userService.login(_user.getUsername(), _user.getPassword());
return auth;
} catch (Exception e) {
auth = new Auth();
return auth;
}
}

@CrossOrigin(origins = "http://localhost:4200")
@GetMapping("/{userId}")
public User getUserById(@PathVariable int userId) {
try{
user = userService.getUserById(userId);
return user;
}
catch (Exception e){
user = new User();
return user;
}
}
}

存储库:

package com.spring.timelybackend.repository;

import com.spring.timelybackend.model.Auth;
import com.spring.timelybackend.model.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Service;

@Service
public interface UserRepository extends CrudRepository<User, Integer> {
Auth login(String username, String password);
User findUserByUsername(String username);
User getUserById(Integer user_id);
}

服务:

package com.spring.timelybackend.services;

import com.spring.timelybackend.model.Auth;
import com.spring.timelybackend.model.User;
import com.spring.timelybackend.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

@Autowired
private UserRepository userRepo;

public Auth login(String username, String password){ return userRepo.login(username, password); }
public User getUserById(int user_id) { return userRepo.getUserById(user_id); }
}

型号:

package com.spring.timelybackend.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String username;
private String password;
private String firstname;
private String lastname;
private String email;

public User (){

}

public User(String username, String password, String firstname, String lastname, String email){
this.username = username;
this.password = password;
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
}

//region getters & setters
public Integer getId(){
return id;
}

public void setId(Integer id){
this.id = id;
}

public String getUsername(){
return username;
}

public void setUsername(String username){
this.username = username;
}

public void setPassword(String password){
this.password= password;
}

public String getPassword(){
return password;
}

public void setFirstname(String firstname){
this.firstname = firstname;
}
public String getFirstname(){
return firstname;
}

public void setLastname(String lastname){
this.lastname = lastname;
}
public String getLastname(){
return lastname;
}

public void setEmail(String email){
this.email = email;
}

public String getEmail(){
return email;
}

//endregion
}

验证模型:

package com.spring.timelybackend.models;

import java.util.UUID;

public class Auth{
private UUID token;
private Integer userId;

public Auth(UUID token, Integer userId){
this.token = token;
this.userId = userId;
}

public Auth(){

}

public UUID getToken() {
return token;
}

public void setToken(UUID token) {
this.token = token;
}

public Integer getUserId() {
return userId;
}

public void setUserId(Integer userId) {
this.userId = userId;
}
}

错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.spring.timelybackend.model.Auth com.spring.timelybackend.repository.UserRepository.login(java.lang.String,java.lang.String)! No property login found for type User!

Pom.xml

<?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>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.spring</groupId>
<artifactId>timely-backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>timely-backend</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</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-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

最佳答案

消息最后:

No property login found for type User!

考虑将方法及其实现放入服务中,因为属性必须存在才能放入存储库中。

关于java - Springboot UnsatisfiedDependencyException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58303832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com