2
I have a class called CoreFiltroSearch
that has a list of ICoreFiltro
calling for filtros
. Follows the class and interface:
public class CoreFiltroSearch {
private List<ICoreFiltro> filtros = new ArrayList<>();
private String order = "asc";
private String sort = "id";
private Integer page = 0;
private Integer size = 40;
}
public interface ICoreFiltro {
String getChave();
String getValor();
Boolean isQFilter();
Boolean isDateFilter();
}
From the list filtros
, need to create a String
using the fields getChave()
and getvalor()
who are of the element ICoreFiltro
.
I tried using a solution I found on a blog, but I can’t even upload the server using this solution, put the question here, the solution I used in an attempt to create this String
was that:
private String montarQueryString(CoreFiltroSearch filter) {
return filter.getFiltros().stream()
.map(p -> urlEncodeUTF8(p.getChave()) + "=" + urlEncodeUTF8(p.getValor()))
.reduce((p1, p2) -> p1 + "&" + p2)
.map(s -> "?" + s).orElse("");
}
static String urlEncodeUTF8(String s) {
try {
return URLEncoder.encode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new UnsupportedOperationException(e);
}
}
If anyone can help me with a better solution I appreciate, but I think there’s no problem with that logic.
I did it last week, but without "reducing". I just peed and collected at the end. I will pass the project link later explain in a reply
– Jefferson Quesado
https://gitlab.com/geosales-open-source/tc-http-conn/blob/master/src/main/java/br/com/softsite/httpconn/HttpConn.java in the method
serializeFormContent
. I’m having difficulty sending the link of the exact line because the smartphone browser does not help, but it is in this method that I do the magic.– Jefferson Quesado