0

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?
2
  • I think you mean query strings more specifically than URLs. Constructing an arbitrary URL from an object would be impossible. However, constructing a query string from members of a class would be possible. Commented Mar 14, 2013 at 13:36
  • Also, possible duplicates of stackoverflow.com/questions/1861620/… and stackoverflow.com/questions/1405731/… Commented Mar 14, 2013 at 13:38

1 Answer 1

1

Something like:

String url = "http://base.com/some/path?" + (maxPrice==null ? "" : "maxPrice="+maxPrice); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.