多个GET请求参数与@PathVariable
问题描述:
获得以下错误,同时调用get请求与多个参数要求:http://localhost:8080/find/1/empid/146220多个GET请求参数与@PathVariable
白色标签错误页面
该应用对/错误没有明确的映射,所以你看到这 作为备用。
Tue Aug 01 19:33:35 IST 2017有一个意外的错误 (type = Internal Server Error,status = 500)。名称为参数绑定 不能为null或空!在JDK中< 8,你需要使用@Param为 命名参数,在JDK 8或更好的,一定要与 α参数进行编译;嵌套的例外是java.lang.IllegalArgumentException异常:对参数绑定不能为空或空的名字!在JDK中< 8,你 需要使用@Param命名参数,在JDK 8或更好,一定 与α参数进行编译。
Demo.java
@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);
}
答
你需要指定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
方法以及
例子:
@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]}")
感谢凯尔,现在的工作完美。 – ravi