【发布时间】:2018-07-19 23:59:06
【问题描述】:
我开始使用 Spring Boot。我的目标是进行有限的搜索,从数据库中检索数据。我想在url的查询中添加多个参数。
到目前为止,我能够使用搜索:http://localhost:8080/wsr/search/ 来全面搜索数据库中的数据。但我想要的是在浏览器的 url 中添加参数的几个条件下分隔搜索,例如:
- http://localhost:8080/data/search/person?name=Will&address=Highstreet&country=UK
- http://localhost:8080/data/search/person?name=Will&name=Angie
- http://localhost:8080/data/search/person?name=Will&name=Angie&country=UK
我发现的问题是我找不到处理多个条件的方法。我要做的唯一事情是:
我在网上冲浪,但没有针对这个确切问题的结果,信息太多但无法找到。
我的代码是:
@Entity
@Table(name = "person")
public class Person {
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
@Column(name = "address")
private String address;
@Column(name = "country")
private String country;
public Value() {
}
public Value(int id, String name, String address, String country) {
this.id = id;
this.name = name;
this.address = address;
this.country = country;
}
//all getters and setters
}
public class Implementation {
@Autowired
private DataBase dataBase;
public List<Value> findById(@PathVariable final int id) {
return dataBase.findById(id);
}
public List<Value> findByName(@PathVariable final String name) {
return dataBase.findByName(name);
}
public List<Value> findByAddress(@PathVariable final String address) {
return dataBase.findByAddress(address);
}
public List<Value> findByCountry(@PathVariable final String country) {
return dataBase.findByCountry(country);
}
}
//@Component
@RepositoryRestResource(collectionResourceRel = "person", path = "data")
public interface DataBase extends JpaRepository<Value, Integer>{
public List<Value> findAll();
@RestResource(path = "ids", rel = "findById")
public List<Value> findById(@Param("id") int id) throws ServiceException;
@RestResource(path = "name", rel = "findByName")
public List<Value> findByName(@Param("name") String name) throws ServiceException;
@RestResource(path = "address", rel = "findByAddress")
public List<Value> findByAddress(@Param("address") String address) throws ServiceException;
@RestResource(path = "country", rel = "findByCountry")
public List<Value> findByCountry(@Param("country") String country) throws ServiceException;
}
希望您能帮助我正确地了解应该做或做错的事情。如果可能的话,一些代码也将受到高度赞赏。
最好的问候
【问题讨论】:
标签: spring-boot spring-data spring-data-rest