【发布时间】:2016-06-29 14:49:51
【问题描述】:
我已经用 spring 创建了项目。
dependencies {
compile('org.springframework.boot:spring-boot-starter-cache')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-data-rest')
compile('org.springframework.boot:spring-boot-devtools')
compile('org.projectlombok:lombok')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.codehaus.groovy:groovy')
runtime('com.h2database:h2')
runtime('mysql:mysql-connector-java')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.restdocs:spring-restdocs-mockmvc')
}
application.properties:
spring.data.rest.base-path=/api
spring.datasource.url=jdbc:mysql://localhost/secret_backend
spring.datasource.username=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
以及实体类:
package com.app.Entity
import lombok.Data
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.Id
import javax.persistence.Table
@Data
@Entity
@Table(name = "cities")
public class City {
private @Id @GeneratedValue Long id;
private String slug;
private String title;
private String titleShort;
private City() {}
/*public String getSlug(){
return slug;
}*/
public City(String slug) {
this.slug = slug;
}
}
当我导航到 localhost:8080/api/cities 时,我没有看到实际的数据表单数据库:
{
"_embedded": {
"cities": [
{
"_links": {
"self": {
"href": "http://localhost:8080/api/cities/7"
},
"city": {
"href": "http://localhost:8080/api/cities/7"
}
}
},
{
"_links": {
"self": {
"href": "http://localhost:8080/api/cities/8"
},
"city": {
"href": "http://localhost:8080/api/cities/8"
}
}
},
...
仅当我向实体添加 getter 时,我才能看到数据,但从 lombok 文档中,@Data 注释必须为所有实体属性生成 getter 和 setter。
【问题讨论】:
-
你检查实际输出了吗?龙目岛乐器上课了吗?此外,lombok 仅在编译时而不是运行时需要。
-
实际输出?也许一些关于 java(spring) dummy 的信息?而lombok在compile('org.projectlombok:lombok')
-
我的意思是,检查已编译的 .class 文件并查看方法是否已添加到其中(使用 java 反编译器或上面的“javap”命令)。编译依赖意味着它将包含在生成的工件中,这将被提供范围(您需要在 gradle 中使用插件)尽管这并不重要。
-
我检查了编译实体,没有 getter 和 setter pastie.org/10759960 所以看来 lombock 不起作用
-
您应该尝试在您的班级中单独运行 lombok。它是一个注释预处理器,如果操作不正确,可能会干扰其他预处理器。
标签: java spring hibernate jpa lombok