Home > front end >  Consider defining a bean of type '...' in your configuration when using JPARepository
Consider defining a bean of type '...' in your configuration when using JPARepository

Time:01-11

Ive been trying to follow a few basic examples to create a rest api to connect to a local mysql server

However I keep running to this error

Consider defining a bean of type 'org.example.database.MessagesRepository' in your configuration.

Here is my current project layout enter image description here

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>

    <groupId>org.example</groupId>
    <artifactId>messagesApi</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <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>
    </dependencies>

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


</project>

Main.java

package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@EnableAsync
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);

    }
}

MessagesApi

package org.example.api;

import org.example.models.Messages;
import org.example.database.MessagesRepository;
import org.example.models.MessagesRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/chats")
public class MessagesApi {
@Autowired
MessagesRepository messagesRepository;

    @GetMapping("id")
    public ResponseEntity<Messages> getAllChatsById(@RequestBody MessagesRequest messageRequest) {

        return new ResponseEntity<>(messagesRepository.findByTextId(messageRequest.getTextId()), HttpStatus.OK);
    }

    @GetMapping("username")
    public ResponseEntity< List<Messages>> getAllChatsByUserName(@RequestBody MessagesRequest messageRequest) {
        return new ResponseEntity<>(messagesRepository.findByUserName(messageRequest.getUserName()), HttpStatus.OK);
    }
    @PostMapping
    public ResponseEntity<Long> postMessage(@RequestBody Messages message ){
        //make Id
        Long id = 1234L;
        message.setTextId(id);
        messagesRepository.save(message);
        return new ResponseEntity<>(id, HttpStatus.CREATED);

    }
}

MessagesRespository

package org.example.database;

import org.example.models.Messages;
import org.springframework.data.repository.JpaRepository;

import java.util.List;
public interface MessagesRepository extends JpaRepository<Messages, Long> {
    List<Messages> findByUserName(String userName);
    Messages findByTextId(Long textId);
}

applications.properties

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=passWord
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

Ive tried adding @EnableJpaRepositories But kept getting ``Consider defining a bean named 'entityManagerFactory' in your configuration.

When removing exclude={DataSourceAutoConfiguration.class} I would get

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

I am not sure where to move forward from here

CodePudding user response:

public interface MessagesRepository extends JpaRepository<Messages, Long> {

rather should be

@Repository
public interface MessagesRepository extends JpaRepository<Messages, Long> {

to make below code work

@Autowired

    MessagesRepository messagesRepository;

also exclude from main class

exclude={DataSourceAutoConfiguration.class} 

CodePudding user response:

Sometimes spring is not able to scan your entity classes or your configuration classes so use this it will work fine

@SpringBootApplication
@ComponentScan({"org.example"})
@EntityScan("org.example")
@EnableJpaRepositories("org.example")
public class WebServiceApplication extends SpringBootServletInitializer {

}
  • Related