如何在使用 Spring Boot 时使用 Rest Service &春季数据 Jpa [英] How to use Rest Service while using Spring Boot & Spring Data Jpa

查看:84
本文介绍了如何在使用 Spring Boot 时使用 Rest Service &春季数据 Jpa的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 spring 数据 jpa 开发用于休息服务的 spring 启动应用程序.我跟着老师看了很多答案,但我无法修复我的休息服务.

Im working on a spring boot application for rest service with using spring data jpa. I followed instructors and read much answers but I couldn't fix my rest service.

这里是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

Here is `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);
}

}

`

这里是 Service.class`

Here is Service.class`

package tr.kasim.Service;

 import java.util.List;

import tr.kasim.Model.Personel;

public interface PersonelService {

List<Personel> findAll();
}

`

这里是 ServiceImplemention.class

Here is ServiceImplemantion.class

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();
}

}

这里是 Dao.class

Here is Dao.class

    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();
}

最后是我的 application.properties

Lastly here is my application.properties

#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

我不确定 componentScan.当我阅读答案时,我发现有人提到了它,但我尝试了,但仍然一无所获.请告诉我我失败的地方.最好的问候.

Im not sure about componentScan. When I read answers I discovered someone mentioned about it but I tried and I got still nothing. Please show me where I failed. Best Regards.

我更新了 Application.class,现在我可以部署项目,但休息服务仍然无法工作.

I updated Application.class, now I can deploying project but rest service not working still.

推荐答案

您如何尝试 ComponentScan?这里的问题似乎是你的包结构是这样的:

How did you try ComponentScan? The issue here seems that you have a package structure like this:

tr.kasim.Application
  - Application.java
tr.kasim.Service
  - PersonelService.java
  - PersonelServiceImpl.java
tr.kasim.Dao
 - PersonelDao.java

现在,mainClass 位于 tr.kasim.Application 中,它将扫描该包内的 bean 定义(或 tr.kasim.Application).所以,

Now since, the mainClass is in tr.kasim.Application it would scan for bean definitions inside that package (or a sub-package in tr.kasim.Application). So,

  • 要么将 mainClass 移出到像 tr.kasim 这样的父包,要么

  • either you move the mainClass out to a parent-package like tr.kasim, or

使用 @ComponentScan({ "tr.kasim.Dao", "tr.kasim.Service", "tr.kasim.Application" }) 等等.

-- 更新--

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

Based on the discussion so far, I'd suggest taking the first option as that reduces the effort to manually enable scan for entity, repository, etc.

这篇关于如何在使用 Spring Boot 时使用 Rest Service &amp;春季数据 Jpa的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆