带有 spring-boot CLI 的 RepositoryRestResource [英] RepositoryRestResource with spring-boot CLI
问题描述
我正在尝试基于带有 spring-boot 和 spring-data-rest 的 JPA 存储库实现一个简单的 REST 服务.(请参阅此教程)如果将以下代码与等级:
I'm trying to implement a simple REST service based on a JPA repository with spring-boot and spring-data-rest. (see this tutorial) The following code works quite well if a use it with gradle:
package ch.bfh.swos.bookapp
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.context.annotation.*
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration
import org.springframework.data.jpa.repository.config.EnableJpaRepositories
import org.springframework.context.annotation.Import
import org.springframework.data.rest.core.annotation.RepositoryRestResource
import org.springframework.data.repository.CrudRepository
import javax.persistence.*
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableJpaRepositories
@Import(RepositoryRestMvcConfiguration.class)
class Application {
static void main(String[] args) {
SpringApplication.run Application, args
}
}
@RepositoryRestResource(path = "authors")
interface AuthorRepository extends CrudRepository<Author, Long> {}
@Entity
class Author {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id
private String firstname
private String lastname
}
为了让事情更简单,我尝试了使用 spring boot CLI(spring run"命令)的相同代码.
To make things even simpler i tried the same code with the spring boot CLI ("spring run" command).
package ch.bfh.swos.bookapp
@Grab("spring-boot-starter-data-jpa")
@Grab("spring-boot-starter-data-rest")
@Grab("h2")
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration
import org.springframework.data.jpa.repository.config.EnableJpaRepositories
import org.springframework.context.annotation.Import
import org.springframework.data.rest.core.annotation.RepositoryRestResource
import org.springframework.data.repository.CrudRepository
import javax.persistence.*
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableJpaRepositories
@Import(RepositoryRestMvcConfiguration.class)
@RepositoryRestResource(path = "authors")
interface AuthorRepository extends CrudRepository<Author, Long> {}
@Entity
class Author {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id
private String firstname
private String lastname
}
不幸的是,这似乎不起作用.@RepositoryRestResource 似乎不会被自动识别,例如@休息控制器.保留@Configuration 部分,服务器会启动,但不会像使用 gradle 那样创建 REST 存储库.
Unfortunately this does not seem to work. @RepositoryRestResource seems not to be automatically recognized like e.g. @RestController. Leaving the @Configuration part in, the server starts up, but the REST repository is not created as if it would with gradle.
有人知道是否可以使用 spring-boot CLI 创建 RepositoryRestResource 以及正确的代码应该是什么样子?
Does anybody know if it is possible to create a RepositoryRestResource with the spring-boot CLI and how the correct code should look like?
推荐答案
您需要一个类定义来将 @Import
挂起(在类上而不是接口上的其他注释是多余的).
You need a class definition to hang the @Import
from (the other annotations that go on a class and not an interface are redundant).
更新:和(更重要的是)Hibernate 无法找到带注释的类,除非它们实际上在文件中(它反射性地分析字节码而不是类定义).因此,您可以通过启动应用程序并以这种方式运行它来使您的应用程序正常工作:
Update: and (more importantly) Hibernate can't find your annotated classes unless they are actually in a file (it analyzes the byte code not the class definition reflectively). So you can make you app work by jarring it up and running it that way:
$ spring jar app.jar app.groovy
$ java -jar app.jar
这是该应用程序的一个较短版本,删除了所有多余的内容:
Here's a shorter version of the app that works like that with all the redundant stuff removed:
package bookapp
@Grab("spring-boot-starter-data-jpa")
@Grab("spring-boot-starter-data-rest")
@Grab("h2")
import org.springframework.data.rest.core.annotation.RepositoryRestResource
import org.springframework.data.repository.CrudRepository
import javax.persistence.*
@Configuration
class App {}
@RepositoryRestResource(path = "authors")
interface AuthorRepository extends CrudRepository<Author, Long> {}
@Entity
class Author {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id
String firstname
String lastname
}
这篇关于带有 spring-boot CLI 的 RepositoryRestResource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!