gpt4 book ai didi

java - spring boot 存储库抛出异常

转载 作者:行者123 更新时间:2023-12-01 17:46:46 25 4
gpt4 key购买 nike

我是 Spring Boot 新手。
我已经创建了我的网络应用程序,如下所示。
SampleApplication.java

@SpringBootApplication
public class SampleApplication implements CommandLineRunner {

@Autowired
DataSource dataSource;

@Autowired
private UserRepository userRepository;

Logger logger = LoggerFactory.getLogger(SampleApplication.class);


public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}

@Transactional(readOnly = true)
@Override
public void run(String... args) throws Exception {
UserLoginController userLoginController = new UserLoginController();

System.out.println("DATASOURCE = " + dataSource);
logger.debug("\nUser: "+userRepository.findByName("user").getName());
logger.debug(userLoginController.getUserInfo("user","pword"));
}
}

UserRepository.java

@EnableJpaRepositories
@Repository
public interface UserRepository extends CrudRepository<User,Long> {

User findByName(String name);

}

User.java

@Entity
@Table(name="USER")
public class User
implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name="USER_ID_GENERATOR", sequenceName="USER_SEQ")
@GeneratedValue(strategy=GenerationType.AUTO, generator="USER_ID_GENERATOR")
private Long id;
@Column(name="NAME")
private String name;
@Column(name="PASSWORD")
private Long password;
public Long getId()
{
return this.id;
}

public void setId(Long id)
{
this.id = id;
}
public String getName()
{
return this.name;
}

public void setName(String name)
{
this.name = name;
}

public String getPassword()
{
return this.password;
}

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

UserLoginController.java

@Controller
public class UserLoginController {
Logger logger = LoggerFactory.getLogger(UserLoginController.class);

@RequestMapping(value = "/userLogin**", method = RequestMethod.POST)
public @ResponseBody String getUserInfo(@RequestParam("mobile") String uName, @RequestParam("pin") String pWord){
logger.debug("UserLoginController | uName: " + uName + " pWord: " + pWord);

UserLoginService userLoginService = new UserLoginService();

String user = null;

try {
logger.debug("UserLoginController | Validate user. uName: " + uName + " pWord: " + pWord);
user = userLoginService.validateSysUser(uName, pWord);
} catch (Exception e) {
logger.debug("UserLoginController | Validate user Exception: " + e.getMessage()+", "+e.getStackTrace());
}

if (user != null) {
logger.debug("UserLoginController | Returning aircash user");
return user;
} else {
logger.debug("UserLoginController | Returning NULL");
return null;
}

}
}

UserLoginService.java

public class UserLoginService {
Logger logger = LoggerFactory.getLogger(UserLoginService.class);

@Autowired
DataSource dataSource;

@Autowired
private UserRepository userRepository;

public String validateSysUser(String uName, String pWord) throws Exception{
logger.debug("UserLoginService | uName: "+uName+" pWord: "+pWord);

User user = new User();

String auser = userRepository.findByName(uName).getName();


if ((auser != null) && (validatePword(user.getPassword(),pWord)){
logger.debug("UserLoginService | User found: "+user.getName());
return auser;
}else {
logger.debug("UserLoginService | User not found");
return null;
}
}

public static boolean validatePword(String pWord,String pWord2){
return BCrypt.checkpw(pWord2,pWord);
}
}

问题是,当我运行应用程序时,这部分给了我预期的响应

logger.debug("\nUser: "+userRepository.findByName("user").getContactNo());

但是,下一行抛出异常。我也尝试使用 postman 调用 Controller 。但同样的异常(exception)也出现了。

logger.debug(userLoginController.getUserInfo("user","pword"));

异常是从UserLoginService.java类引发的。异常消息显示为 null

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 http://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.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>robicash</groupId>
<artifactId>distributorcommision</artifactId>
<version>1.0</version>
<name>distributorcommission</name>
<description>commision for distributors</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mindrot</groupId>
<artifactId>jbcrypt</artifactId>
<version>0.3m</version>
</dependency>
<!--<dependency>-->
<!--<groupId>com.oracle</groupId>-->
<!--<artifactId>ojdbc8</artifactId>-->
<!--<version>11.2.0.4</version>-->
<!--</dependency>-->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

我不明白为什么同一个存储库在调用时会给出两个结果。

你能告诉我如何让它工作吗?

最佳答案

您无法实例化 UserLoginService userLoginService = new UserLoginService();

你必须将它注入(inject)到 Controller 中:

@Autowired
UserLoginService userLoginService;

Spring 只会管理由 Spring 实例化然后注入(inject)的服务。

是的,Kes 是部分正确的。必须在UserLoginService中添加@Service或@Component注解:

@Service
public class UserLoginService {

关于java - spring boot 存储库抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54308038/

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