fweigl
fweigl

Reputation: 22018

Object to Rest-URL

The user can set a number of values (which are optional) for a search query that will be passed on to a REST api. So I create a POJO to hold all the set values like

class Search{

  private String minPrice; 
  private String maxPrice; 
  private String category; 
  // etc.pp.

}

When I construct the URL for the api call, i have to check wether that value is set all the time like

if (search.getMinPrice() != null){

  url.append("&minprice=" + search.getMinPrice());

}

Is there a more convenient/ elegant way to do this

  1. with pure Java, just a way to "do sth if sth is not null"
  2. or even a library/tool that lets you construct Urls from objects?

Upvotes: 0

Views: 254

Answers (1)

CodeChimp
CodeChimp

Reputation: 8154

Something like:

String url = "http://base.com/some/path?" + (maxPrice==null ? "" : "maxPrice="+maxPrice);

Upvotes: 1

Related Questions