Java 具有@PathVariable的多个get请求参数
使用get请求中的多个参数调用请求时出现以下错误: 白标错误页 此应用程序没有/error的显式映射,因此您可以看到 这是一种退路 2017年8月1日星期二19:33:35发生意外错误 (类型=内部服务器错误,状态=500)。参数绑定的名称 不能为null或空!在jdk<8时,需要使用@Param 命名参数,在JDK 8或更好的版本上,请确保使用 -参数。;嵌套异常为java.lang.IllegalArgumentException:参数绑定的名称不能为null或空!在jdk<8上,您可以 在JDK 8或更高版本上,需要对命名参数使用@Param,请确保 使用-parameters编译 Demo.javaJava 具有@PathVariable的多个get请求参数,java,spring-boot,spring-data,spring-data-jpa,Java,Spring Boot,Spring Data,Spring Data Jpa,使用get请求中的多个参数调用请求时出现以下错误: 白标错误页 此应用程序没有/error的显式映射,因此您可以看到 这是一种退路 2017年8月1日星期二19:33:35发生意外错误 (类型=内部服务器错误,状态=500)。参数绑定的名称 不能为null或空!在jdk
@Entity
public class Demo {
@Id
private Long id;
private String name;
private String value;
//getter -setter
}
DemoApplication.java
@SpringBootApplication
@RestController
public class DemoApplication {
@Autowired
private DemoRepository repository;
@RequestMapping(method=RequestMethod.GET, value="/find/{id}/{name}/{value}")
public Demo find(@PathVariable Long id, @PathVariable String name, @PathVariable String value){
return repository.findByIdAndNameAndValue(id, name, value);
}
}
DemoRepository.java
public interface DemoRepository extends CrudRepository<Demo, Long>{
@Query("select d from Demo d where d.id = :id and d.name = :name and d.value = :value")
Demo findByIdAndNameAndValue(Long id, String name, String value);
}
公共接口DemoRepository扩展了Crudepository{
@查询(“从Demo d中选择d,其中d.id=:id和d.name=:name和d.value=:value”)
演示findByIdAndNameAndValue(长id、字符串名称、字符串值);
}
您需要指定PathVariable
名称
示例:
@RequestMapping(method=RequestMethod.GET, value="/find/{id}/{name}/{value}")
public Demo find(@PathVariable(name = "id") Long id, @PathVariable(name = "name") String name, @PathVariable(name = "value") String value){
return repository.findByIdAndNameAndValue(id, name, value);
}
@Query("select d from Demo d where d.id = :id and d.name = :name and d.value = :value")
Demo findByIdAndNameAndValue(@Param("id") Long id, @Param("name") String name, @Param("value") String value);
在查询
方法中也是如此
示例:
@RequestMapping(method=RequestMethod.GET, value="/find/{id}/{name}/{value}")
public Demo find(@PathVariable(name = "id") Long id, @PathVariable(name = "name") String name, @PathVariable(name = "value") String value){
return repository.findByIdAndNameAndValue(id, name, value);
}
@Query("select d from Demo d where d.id = :id and d.name = :name and d.value = :value")
Demo findByIdAndNameAndValue(@Param("id") Long id, @Param("name") String name, @Param("value") String value);
您需要指定
PathVariable
名称
示例:
@RequestMapping(method=RequestMethod.GET, value="/find/{id}/{name}/{value}")
public Demo find(@PathVariable(name = "id") Long id, @PathVariable(name = "name") String name, @PathVariable(name = "value") String value){
return repository.findByIdAndNameAndValue(id, name, value);
}
@Query("select d from Demo d where d.id = :id and d.name = :name and d.value = :value")
Demo findByIdAndNameAndValue(@Param("id") Long id, @Param("name") String name, @Param("value") String value);
在查询
方法中也是如此
示例:
@RequestMapping(method=RequestMethod.GET, value="/find/{id}/{name}/{value}")
public Demo find(@PathVariable(name = "id") Long id, @PathVariable(name = "name") String name, @PathVariable(name = "value") String value){
return repository.findByIdAndNameAndValue(id, name, value);
}
@Query("select d from Demo d where d.id = :id and d.name = :name and d.value = :value")
Demo findByIdAndNameAndValue(@Param("id") Long id, @Param("name") String name, @Param("value") String value);
您可能应该将查询更改为:
@Query("select d from Demo d where d.id = ?#{[0]} and d.name = ?#{[1]} and d.value = ?#{[2]}")
您可能应该将查询更改为:
@Query("select d from Demo d where d.id = ?#{[0]} and d.name = ?#{[1]} and d.value = ?#{[2]}")