I am new to Spring Boot and I’m getting the following error when writing a login validationAPI:
Field userservice in com.Productwebsite.controller.UserController required a bean of type ‘com.Productwebsite.service.UserService’ that could not be found.
The injection point has the following annotations:
– @org.springframework.beans.factory.annotation.Autowired(required=true)
Controller class:
package com.Productwebsite.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.Productwebsite.service.UserService;
@RestController
public class UserController {
@Autowired
private UserService userservice;
@GetMapping("user/{username}/{password}")
public int UserLogin(@PathVariable("username")String username1, @PathVariable("password")String password1) {
int flag = userservice.loginValidation(username1, password1);
if (flag == 0) {
return 0;
}
return flag;
}
}
Modelclass:
package com.Productwebsite.model;
public class users {
String username;
String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "users [username=" + username + ", password=" + password + "]";
}
public users(String username, String password) {
super();
this.username = username;
this.password = password;
}
public users() {
super();
// TODO Auto-generated constructor stub
}
}
Service interfface:
package com.Productwebsite.service;
import org.springframework.stereotype.Repository;
@Repository
public interface UserService {
public int loginValidation(String username,String password);
}
Serviceimplementation class:
package com.Productwebsite.serviceImp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.stereotype.Service;
import com.Productwebsite.Dbutil.dbutil;
import com.Productwebsite.service.UserService;
@Service
public class ServiceImp implements UserService {
Connection connection;
int flag = 0;
public ServiceImp() throws SQLException {
connection = dbutil.getConnection();
}
@Override
public int loginValidation(String username, String password) {
try {
PreparedStatement statement
= connection.prepareStatement("SELECT * FROM users WHERE username='"+username+"'");
ResultSet rs=statement.executeQuery();
while(rs.next()) {
if (rs.getString(1).equals(username)&&rs.getString(2).equals(password)) {
flag=1;
}
else {
System.out.println("Invalid username/password");
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
}
>Solution :
Try Add componentscan to com.Productwebsite once and restart the app.
@ComponentScan(basePackages = "com.Productwebsite")
@SpringBootApplication
public class YourMainClass{
}
Try this as second stage. if the above one is not working
change annotation by adding the bean name
@Repository("userservice")