如何在使用Spring Boot时使用Rest服务&;Spring数据Jpa

如何在使用Spring Boot时使用Rest服务&;Spring数据Jpa,rest,spring-boot,spring-data-jpa,spring-tool-suite,Rest,Spring Boot,Spring Data Jpa,Spring Tool Suite,我正在使用SpringDataJPA为rest服务开发SpringBoot应用程序。我跟随导师,读了很多答案,但我无法修复我的rest服务 这是application.class package tr.kasim.Application; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import or

我正在使用SpringDataJPA为rest服务开发SpringBoot应用程序。我跟随导师,读了很多答案,但我无法修复我的rest服务

这是
application.class

package tr.kasim.Application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;


@SpringBootApplication
@EnableJpaRepositories("tr.kasim.Dao")
@EntityScan("tr.kasim.Model")
@ComponentScan({"tr.kasim.Service", "tr.kasim.Application" })
public class Application {

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

}
这是restcontroller.class

package tr.kasim.Controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import tr.kasim.Service.PersonelService;
import tr.kasim.Model.Personel;


@RestController
public class STRestController {

@Autowired
public PersonelService personelService;

@RequestMapping(value = "/api/personels", method = RequestMethod.GET)
public ResponseEntity<List<Personel>> getPersonels(){
    List<Personel> personels = personelService.findAll();
    return ResponseEntity.ok().body(personels);
}

}
我对组件扫描没有把握。当我阅读答案时,我发现有人提到了这件事,但我试了一下,还是一无所获。请告诉我哪里失败了。致以最良好的祝愿


我更新了Application.class,现在我可以部署project,但rest服务无法正常工作。

您是如何尝试组件扫描的?这里的问题似乎是您有这样的包结构:

tr.kasim.Application
  - Application.java
tr.kasim.Service
  - PersonelService.java
  - PersonelServiceImpl.java
tr.kasim.Dao
 - PersonelDao.java
现在,由于
main类
位于
tr.kasim.Application
中,它将扫描该包(或
tr.kasim.Application
中的子包)中的bean定义。所以

  • 您可以将
    main类
    移出到父包,如
    tr.kasim
    ,或者

  • 使用
    @ComponentScan({“tr.kasim.Dao”、“tr.kasim.Service”、“tr.kasim.Application”})
    等等

--更新--


根据到目前为止的讨论,我建议采用第一个选项,因为这样可以减少手动启用实体、存储库等扫描的工作量。

Hello Sinbad90,首先感谢您的回答。但我之前尝试过这个方法,并出现了错误:tr.kasim.Service.PersonelServiceImpl中的字段personelDao需要一个类型为“tr.kasim.Dao.personelDao”的bean,但找不到该bean。动作:考虑在配置中定义一个类型为“T.Kasim.Da.PosielDaO”的bean。您尝试过将包名<代码> Tr.Kasim.Dao < /C> > < <代码> @ Enable JPARPOILITIOS< <代码>吗?当我在这里写“Enable JaPaSeStIOSITS”(“T.Kasim. Dao”)我犯了以下错误:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“personelServiceImpl”的bean时出错:通过字段“personelDao”表示的未满足的依赖关系;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“personelDao”的bean时出错:调用init方法失败;嵌套异常为java.lang.IllegalArgumentException:不是托管类型:class tr.kasim.Model.PersonelYour Model class
Personel
可能缺少
@Entity
,因此无法将其识别为托管类型。您可以确认这一点,或者共享模型类的代码吗?当然可以!是的。再次感谢你。
package tr.kasim.Service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tr.kasim.Dao.PersonelDao;
import tr.kasim.Model.Personel;

@Service
public class PersonelServiceImpl implements PersonelService {

@Autowired
private PersonelDao personelDao;

@Override
@Transactional
public List<Personel> findAll() {

    return personelDao.findAll();
}

}
    package tr.kasim.Dao;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import tr.kasim.Model.Personel;

@Repository
public interface PersonelDao extends JpaRepository<Personel, Long> {

List<Personel> findAll();
}
#MySql Connection
spring.datasource.url=jdbc:mysql://localhost:3306/exampleproject?verifyServerCertificate=false&useSSL=true
spring.datasource.username=root
spring.datasource.password=*******
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#Jpa/Hibernate
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

#Logging
logging.file=staffTracking.log
logging.level.org.springframework.web=debug
tr.kasim.Application
  - Application.java
tr.kasim.Service
  - PersonelService.java
  - PersonelServiceImpl.java
tr.kasim.Dao
 - PersonelDao.java