![](/img/trans.png)
[英]Get multiple values for same parameter name in request URL - Spring boot
[英]How to get url parameter with spring boot
我正在尝试完成教程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> {
}
只是一个原始库; 和类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
来更改spring.data.rest.base-path=/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中提取索引。 有没有我可能会忘记的配置?
一些可能的线索:
之所以不起作用,是因为在使用具有maven2eclipse功能的Eclipse构建时,要求Lombok代理与IDE一起使用以生成预期的代码。
如果它是纯粹用maven编译的,那会行得通,但是如果没有代理,通过Eclipse和m2e进行的构建将无法工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.