0

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:

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

Marc Tarin
  • 3,109
  • 17
  • 49
Json
  • 1
  • 2
  • Possible duplicate of [How to make an advanced search with Spring Data REST?](https://stackoverflow.com/questions/36222830/how-to-make-an-advanced-search-with-spring-data-rest) – Marc Tarin Feb 09 '18 at 09:49

1 Answers1

0

You can use @RequestParam("nameParameter")annotation to map all the parameters you want. Let's say you have url like :

http://localhost:8080/data/search/person?name=Will&country=UK

then you can have an api like:

...
@RequestMapping(value = "/person")
public String api(@RequestParam("name") String name, @RequestParam("country") String country)
...
NiVeR
  • 9,644
  • 4
  • 30
  • 35
  • In the question there is no contrroller layer at all. OP is using spring-data-rest. How does this help? – pvpkiran Feb 09 '18 at 09:06
  • To me it seemed that the OP was confused and I proposed an alternative solution to the problem. – NiVeR Feb 09 '18 at 09:14
  • What do you mean with "_In the question there is no contrroller layer at all. OP is using spring-data-rest"_, sorry but don't understand. I'm a completely newbie in this, Spring Boot. The suggestion is to create a new method, but the problem I found here is that can't know which parameters will be included by the user in the query. Could be: - person?name=Will&country=UK - person?name=Will&name=Angie I want all the conditions are fulfilled, don't know the number of "name" params, or "name, address, country" params, the amount of them that will there be or if all of them will be in query. – Json Feb 09 '18 at 09:42
  • @Json In such case you should compose a Json (:D) and send it as a body in a post request to the server, which will map it in the correct object. – NiVeR Feb 09 '18 at 09:45
  • Haha, that's a starting point. I need a Json! Thanks, I will look for how to build that. At this moment I'm completely stuck, several days researching what Spring Boot "configuration" need to use. Do you know about any useful example, do you have a link? I'm almost getting crazy with this. A question, shall I use POST instead of GET request, is it a mandatory condition for the Json? – Json Feb 09 '18 at 09:59