2017-08-07 41 views
1

我正在尝试完成教程https://spring.io/guides/tutorials/react-and-spring-data-rest/如何通过弹簧启动获取url参数

本教程仅创建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; 
    } 
} 

使用龙目岛@Data创建getter和setter方法; EmployeeRepository: package com.vli.react_spring_tutorial;

package com.vli.react_spring_tutorial; 

import org.springframework.data.repository.CrudRepository; 

public interface EmployeeRepository extends CrudRepository<Employee, Long> { 

} 

只是一个粗暴的仓库;和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的端点。

当我尝试的网址:本地主机:8080/API /员工/ 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本地主机:8080/API /员工我得到这个:

{ 
    "_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" 
    } 
    } 
} 

正如人们所看到的,它知道我有多少投入在数据库,但不加载员工的详细信息。

在我看来,春季启动无法从URL中提取索引。有没有这样的配置或缺失,我可以忘记?

一些可能的线索:

  1. 我在一个非常严格的环境尝试此。我工作的公司有很多安全规则。我不能成为我自己的计算机的管理员,有许多防火墙限制,防病毒等。也许这其中一件事可能会导致问题。
  2. 我打开H2控制台,看到数据库被正确填充。
  3. 有趣的是,我的应用程序知道我有多少员工,但无法检索他们的信息。
  4. 我尝试使用curl POST在数据库中插入新员工。它会创建一个新的员工条目,为其分配一个ID,但不能在其上记录数据。
+0

告诉我们你写了什么 – Jeyaprakash

+0

@Jeyaprakash谢谢,我做了一个版本。 –

+0

如果你点击'http:// localhost:8080/api/employees',会发生什么?它会返回3个条目吗? –

回答

0

这个不起作用的原因是因为当使用Eclipse构建maven2eclipse功能时,需要使用Lombok代理与IDE一起生成期望的代码。

如果它是纯粹用maven编译的,但是通过Eclipse构建并且m2e在代理不存在的情况下无法工作,那么这种方法就行得通了。