My server starts up fine but when I use postman to send a request I get a 404 not found. In my server details on console it says "Finished Spring Data repository scanning in 4 ms. Found 0 JPA repository interfaces." I'm just trying to get a simple check to see if a employee is in my MySQL database.
My repository
package com.kingMachine.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.kingMachine.entity.Employees;
@Repository
public interface EmployeeRepository extends JpaRepository<Employees, String>{
@Query("Select S from Employees S where S.id = ?1 and S.password = ?2")
Employees login(String id, String password);
}
My Controller
package com.kingMachine.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.kingMachine.entity.Employees;
import com.kingMachine.repository.EmployeeRepository;
public class EmployeeController {
@Autowired
EmployeeRepository employeeRepository;
//login info finds user by id and password in a query
@RequestMapping(value="/login",
produces=MediaType.APPLICATION_JSON_VALUE,
method=RequestMethod.POST
)
public ResponseEntity<Employees>login(@RequestBody Employees employees){
Employees employeeLogin = employeeRepository.login(employees.getId(), employees.getPassword());
if(employeeLogin != null) {
return new ResponseEntity<>(employeeLogin, HttpStatus.OK);
}else {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
}
}
Main Application
package com.kingMachine.login;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages="com.kingMachine")
public class LoginApplication {
public static void main(String[] args) {
SpringApplication.run(LoginApplication.class, args);
}
}
Finally POM
<?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.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kingMachine</groupId>
<artifactId>login</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>login</name>
<description>KingMachine Login</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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.2.Final</version>
<type>pom</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
CodePudding user response:
Specify LoginApplication annotations
@SpringBootApplication(scanBasePackages = "com.kingMachine")
@EnableJpaRepositories("com.kingMachine.repository")
@EntityScan("com.kingMachine.entity")
public class LoginApplication {
and add @Controller annotation to EmployeeController so it can be scanned
@Controller
public class EmployeeController {
CodePudding user response:
Since your main class is not in parent package of your repositories. You need to use @EnableJpaRepositories
. Or just move you main class into com.kingMachine