I am starting working with Spring Boot. My aim is to make a limited search retrieving data from a database. I want to add multiple parameters in the query of the url.
So far I was able using the seek: http://localhost:8080/wsr/search/, to get a full search of the data in the database. But what I want is delimit the search under several conditions adding parameters in the url in the browser as for instance:
- 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
The problem I found is that I can't find the way to work with more than one condition. The only thing I got to make it work, is:
I surfed the web but no results for this exact problem, too much information but impossible to find this.
The code I have is:
@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;
}
Hope you can help me putting me in the correct way of what should do or is wrong. If possible some code will also be highly appreciated.
Best regards