[英]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.