我正在尝试完成教程https://spring.io/guides/tutorials/react-and-spring-data-rest/。
本教程仅创建DatabaseLoader:
package com.vli.react_spring_tutorial;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class DatabaseLoader implements CommandLineRunner {
private final EmployeeRepository repository;
@Autowired
public DatabaseLoader(EmployeeRepository repository) {
this.repository = repository;
}
@Override
public void run(String... strings) throws Exception {
this.repository.save(new Employee("Frodo", "Baggins", "ring bearer"));
this.repository.save(new Employee("Leandro", "Santos", "Dev"));
this.repository.save(new Employee("Weverton", "Dias", "Dev"));
}
}
初始化数据库H2;实体员工:
package com.vli.react_spring_tutorial;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import lombok.Data;
@Data
@Entity
public class Employee {
private @Id @GeneratedValue Long id;
private String firstName;
private String lastname;
private String description;
private Employee() {}
public Employee(String firstName, String lastname, String description) {
this.firstName = firstName;
this.lastname = lastname;
this.description = description;
}
}
使用Lombok @Data创建getter和setter; EmployeeRepository: 包com.vli.react_spring_tutorial;
package com.vli.react_spring_tutorial;
import org.springframework.data.repository.CrudRepository;
public interface EmployeeRepository extends CrudRepository<Employee, Long> {
}
只是一个crud存储库;和类ReactSpringTutorialApplication:
package com.vli.react_spring_tutorial;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ReactSpringTutorialApplication {
public static void main(String[] args) {
SpringApplication.run(ReactSpringTutorialApplication.class, args);
}
}
项目的主要类。此外,我已将spring.data.rest.base-path=/api
设置为更改api的端点。
当我尝试URL:localhost:8080 / api / employees / 1时,响应为:
{
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/employees/1"
},
"employee" : {
"href" : "http://localhost:8080/api/employees/1"
}
}
}
而不是关于id为1的员工的信息,如预期的那样。在控制台我得到了:
2017-08-07 13:13:29.043 ERROR 7328 --- [nio-8080-exec-1] o.s.d.r.w.RepositoryRestExceptionHandler : Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'undefined'; nested exception is java.lang.NumberFormatException: For input string: "undefined"
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'undefined'; nested exception is java.lang.NumberFormatException: For input string: "undefined"
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:43) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:203) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:187) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.data.repository.support.ReflectionRepositoryInvoker.convertId(ReflectionRepositoryInvoker.java:277) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
at org.springframework.data.repository.support.CrudRepositoryInvoker.invokeFindOne(CrudRepositoryInvoker.java:91) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
at org.springframework.data.rest.core.support.UnwrappingRepositoryInvokerFactory$UnwrappingRepositoryInvoker.invokeFindOne(UnwrappingRepositoryInvokerFactory.java:130) ~[spring-data-rest-core-2.6.6.RELEASE.jar:na]
at org.springframework.data.rest.webmvc.RepositoryEntityController.getItemResource(RepositoryEntityController.java:524) ~[spring-data-rest-webmvc-2.6.6.RELEASE.jar:na]...
如果我使用url localhost:8080 / api / employees我得到了这个:
{
"_embedded" : {
"employees" : [ {
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/employees/1"
},
"employee" : {
"href" : "http://localhost:8080/api/employees/1"
}
}
}, {
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/employees/2"
},
"employee" : {
"href" : "http://localhost:8080/api/employees/2"
}
}
}, {
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/employees/3"
},
"employee" : {
"href" : "http://localhost:8080/api/employees/3"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/employees"
},
"profile" : {
"href" : "http://localhost:8080/api/profile/employees"
}
}
}
有人可以看到,它知道我在数据库中输入了多少,但没有加载员工的详细信息。
在我看来,spring boot无法从URL中提取索引。是否有任何可以忘记的配置或缺席?
一些可能的线索:
答案 0 :(得分:0)
这不起作用的原因是,当使用带有maven2eclipse功能的Eclipse构建时,需要将Lombok代理与IDE一起使用以生成预期的代码。
如果它纯粹是用maven编译的话,这会有效,但是如果没有代理在那里,那么通过Eclipse构建并且m2e不起作用。