简体   繁体   English

反对剩余URL

[英]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. 用户可以为搜索查询设置许多值(可选),这些值将传递给REST api。 So I create a POJO to hold all the set values like 所以我创建了一个POJO来保存所有设置值,例如

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 当我为api调用构造URL时,我必须检查是否一直在设置该值,例如

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" 使用纯Java,只是“如果sth不为null则执行某事”的一种方法
  2. or even a library/tool that lets you construct Urls from objects? 甚至是一个库/工具,可让您从对象构造Urls?

就像是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM