2017-08-01 146 views
0

獲得以下錯誤,同時調用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); 

} 

回答

4

你需要指定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); 
+1

感謝凱爾,現在的工作完美。 – ravi

0

也許你應該查詢更改爲:

@Query("select d from Demo d where d.id = ?#{[0]} and d.name = ?#{[1]} and d.value = ?#{[2]}")