我正在使用Spring boot设置基本的REST,但遇到了一些我不知道如何解决的错误。
希望你们能帮助我:)
我已经尝试了一些注释修复,但是没有成功... 在下面可以找到我的代码。也许依赖性不正确?
UserController:
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>
答案 0 :(得分:0)
您应该这样更改代码:
@Repository
public interface UserRepository extends CrudRepository<User, Integer>
{
Auth findByUsernameAndPassword(String username, String password);
User findUserByUsername(String username);
User getUserById(Integer user_id);
}
这是一个存储库,而不是服务。
您应将Auth类作为关系添加到用户实体中。
@OneToOne(mappedBy = "Auth")
private Auth auth;
还有您的Auth实体。
@OneToOne
private User user;
您正在尝试从方法名称查询创建,但是此方法没有方法实现。 spring documentation for spring data jpa
答案 1 :(得分:0)
在消息末尾:
找不到类型为User的属性登录!
考虑将该方法及其实现放置在服务中,因为必须存在该属性才能将其放置在存储库中。